SlideShare una empresa de Scribd logo
1 de 38
iOS程序设计
-数据持久化
        讲师: 谭奇宇
Email&MSN: qiyutan@hotmail.com
数据持久化的定义
二进制文件
 C语言函数库: fwrite, fread


 iOS平台:NSFileManger, NSData
属性列表
 定义
 具有层次结构的对象列表,可以通过
 NSPropertyListSerialization实现持久化。

 存储格式
 1. 标准XML
 2. 二进制
 3. 兹容旧格式(只读)
属性列表
 支持数据类型
属性列表
 写数据
 NSMutableDictionary *info = [NSMutableDictionary dictionary];

 [info setObject:@"Tom" forKey:@"Player"];
 [info setObject:[NSNumber numberWithInt:3000] forKey:@"Score"];

 [info writeToFile:fileName atomically:YES];
 或者
 NSData *data = [NSPropertyListSerialization dataFromPropertyList:info
 format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
 [data writeToFile:fileName atomically:YES];

 注意:NSDictionary的key对象必须实现NSCopying协议。若作为
 属性列表,必须得是NSString,即使NSNumber都不可以, 因为key
 值在XML中的元素类型只有1个<key>,无法区分类型。
属性列表
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
         <key>Player</key>
         <string>Tom</string>
         <key>Score</key>
         <integer>3000</integer>
</dict>
</plist>
属性列表
 读数据
 NSDictionary *info = [NSDictionary
 dictionaryWithContentsOfFile:fileName];
 或者
 NSData *data = [NSData dataWithContentsOfFile:fileName];
 NSDictionary *info = [NSPropertyListSerialization
                           propertyListFromData:data
                           mutabilityOption:NSPropertyListImmutable
                              format:&format
                           errorDescription:&error];
属性列表
@interface Player : NSObject
@property(retain) NSString *name;
@property(assign) int score;
@end

Player *p = [[[Player alloc] init] autorelease];
p.name = @"Tom";
p.score = 3000;
NSMutableDictionary *info = [NSMutableDictionary dictionary];
[info setObject:p forKey:@"Player"];
[info writeToFile:fileName atomically:YES]; //写操作失败
NSUserDefaults
 以plist格式存储<key, value>配置信息

 存储位置

$HOME/Library/Preferences/<ApplicationBundleIdentifer>.plist
NSUserDefaults
 初始化配置信息

if(![NSStandardUserDefaults objectForKey:@"DefaultLevel"]){
      [NSStandardUserDefaults setObject:[NSNumber
numberWithInt:1] forKey:@"DefaultLevel"];
}
…

NSDictionary *defaultSettings = [NSDictionary
dictionaryWithContentsOfFile:plistPathInBundle];
[NSStandardUserDefaults registerDefaults:defaultSettings];
NSUserDefaults
 不宜存放以下信息
 1. 敏感数据
   比如:帐号,密码
 2. 大数据量信息

 思考
 如何保证程序的配置信息在越狱iOS上不会被恶意修改?
 大数据量该如何存取?
NSJSONSerialization
 iOS 5.0版本引入,替代JSONKit等开源工具

 将NSArray, NSDictionary转化成JSON数据格式

 NSArray, NSDictionary中的元素必须是NSString,
   NSNumber, NSArray, NSDictionary, 或 NSNull

 NSDictionary的key必须是NSString

 用法
  NSArray *players = …;
  [NSJSONSerialization dataWithJSONObject:players
  options:NSJSONWritingPrettyPrinted error:&error];
  …
  players = [NSJSONSerialization JSONObjectWithData:data
  options:NSJSONReadingMutableContainers error:&error];
对象归档
 将对象及对象关系序列化为二进制流

 一种对象持久化通用解决方案
对象归档
 NSCoder
  1. 序列形式
    NSArchiver, NSUnarchiver (iOS不支持)
  2. Key-Value形式
    NSKeyedArchiver, NSKeyedUnarchiver
对象归档
 归档(Archive)

  NSMutableData *data = [NSMutableData data];
  NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
  initForWritingWithMutableData:data];
  [archiver encodeObject:player forKey:@”Player"];
  //…
  [archiver finishEncoding];
  [data writeToFile:archivePath atomically:YES];


  [NSKeyedArchiver archiveRootObject:player
  toFile:archivePath];
对象归档
 解档(Unarchive)
  NSData *data = [NSData dataWithContentsOfFile:archivePath];
  unarchiver = [[NSKeyedUnarchiver alloc]
  initForReadingWithData:data];
  player = [unarchiver decodeObjectForKey:@”player"];
  [unarchiver finishDecoding];
  [unarchiver release];


  player = [NSKeyedUnarchiver
  unarchiveObjectWithFile:archivePath];
对象归档
 NSCoding协议

@interface Player : NSObject<NSCoding>
//…

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
//如果父类就已经实现了NSCoding协议
    //self = [super initWithCoder:aDecoder];
    if(self){
        self.name = [aDecoder decodeObjectForKey:@"Name"];
        self.score = [aDecoder decodeIntForKey:@"Score"];
    }
    return self;
}
对象归档
 NSCoding协议


 - (void)encodeWithCoder:(NSCoder *)aCoder
 {
    //[super encodeWithCoder];
    [aCoder encodeObject:self.name forKey:@"Name"];
    [aCoder encodeInt:self.score forKey:@"Score"];
 }
对象归档
 NSNumber, NSString, NSDate等,以及NSArray,
  NSDictionary, NSSet集合类均实现了NSCoding协议


   NSMutableArray *players = [NSMutableArray array];
   [players addObject:player1];
   …
   //会调用数组中每个player对象的encoder
   [NSKeyedArchiver archiveRootObject:players
   toFile:archivePath];

   //会调用decoder生成每个player对象
   players = [NSKeyedUnarchiver
   unarchiveObjectWithFile:archivePath];
SQLite
 C语言接口,支持通用的SQL操作

 无服务器

 单文件

 内存占用小,速度快,比较可靠

 不适合存储多媒体数据:音视频、图片资源

 并发处理方面比较弱(对手机端影响不大)
SQLite
 接口举例

1. 打开数据库
   int sqlite3_open(constchar*filename,sqlite3**db);

1. 执行SQL语句
   int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void
   *, int, char **, char **), void *context, char **error);

2. 读取查询结果
   int mycallback(void*context,int count, char**values, char**cols);

3. 关闭数据库
   int sqlite3_close(sqlite3 *db);
Core Data
 为何需要使用Core Data
  1. 开发工作量和效率
  2. 后期数据维护和升级
  3. 性能优化
  4. 线程安全
  5. 更好地支持iCloud
Core Data
 数据管理框架,提供一套OC语法的接口

 不是通常意义上的数据库,存储模型可以是SQLite,
 XML等
Core Data
 实体-关系模型
                            NSEntityDescription




                                                   NSAttributeDescription




     NSRelationshipDescription


                                        NSManagedObjectModel
Core Data
 NSManagedObject-描述数据对象的类型
  1. 数据对象均为NSManagedObject类型或派生类型
  2. 利用Xcode可以自动生成表示Player, Team类代码
Core Data
 自动生成的Player类

 @interface Player : NSManagedObject

 @property (nonatomic, retain) NSNumber * name;
 @property (nonatomic, retain) NSString * score;
 @property (nonatomic, retain) NSManagedObject *team;

 @end
Core Data
 自动生成的Player类

 @interface Player : NSManagedObject

 @property (nonatomic, retain) NSNumber * name;
 @property (nonatomic, retain) NSString * score;
 @property (nonatomic, retain) NSManagedObject *team;

 @end

           @implementation Player
           //dynamic指示符用于告诉编译器运行时会生成对应的读取方法
           @dynamic name;
           @dynamic score;
           @dynamic team;

           @end
Core Data
 自动生成的Team类
@class Player;

@interface Team : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *players;
@end

@interface Team (CoreDataGeneratedAccessors)
//声明对应的添加和删除Player的方法,也是运行时才
实现
- (void)addPlayersObject:(Player *)value;    @implementation Team
- (void)removePlayersObject:(Player *)value;
- (void)addPlayers:(NSSet *)values;          @dynamic name;
- (void)removePlayers:(NSSet *)values;       @dynamic players;

@end                                             @end
Core Data
 NSManagedObject遵循KVC
  NSString *name = [managedObject valueForKey:@”name”];
  等价于
  NSString *name = player.name;

 什么情况下需要继承NSManagedContext?
  1. 代码更直观简洁
  2. 添加方法或属性
     比如,可以增加portrait方法,根据player.name属性,
     查询文件返回对应的UIImage对象
Core Data
      读取与存储数据
                        数据对象
     NSManagedObject      …          NSManagedObject


数据对象管理器:增,                                         NSManagedObjectContex
删,改,查,撤销/重
           NSManagedObjectContext             …
                                                            t
做

 数据字典(一般加载合并
 Bundle中的所有model)                NSPersistentStoreCoordinator
 NSManagedObjectMode
          l


          数据文          NSPersistentStore    …      NSPersistentStore
          件
Core Data
 初始化工作

model = [NSManagedObjectModel mergedModelFromBundles:nil]; //nil
表示从mainbundle中加载合并所有model

coordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:model];
 [coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:options error:&error];

context = [[NSManagedObjectContext alloc] init];
context.persistentStoreCoordinator = coordinator;

以上操作,iOS5.0下,等价于
UIManagedDocument *document = [[UIManagedDocument alloc]
initWithFileURL:storeUrl];
Core Data
 增加记录

  player = [NSEntityDescription
            insertNewObjectForEntityForName:@"Player”
            inManagedObjectContext:context];
  team = [NSEntityDescription
            insertNewObjectForEntityForName:@"Team”
            inManagedObjectContext:context];
  team.name = @"TheBest";
  player.name = @"Tom";
  player.score = [NSNumber numberWithInt:3000];
  player.team = team;
  if(![context save:&error]){
            NSLog(@"%@", error);
  }
Core Data
 删除记录
 [context deleteObject:player];

 查询记录
 NSFetchRequest *request = [NSFetchRequest
 fetchRequestWithEntityName:@"Player"];
 request.predicate = [NSPredicate
 predicateWithFormat:@"score > 2000"];
 NSSet *players = [context executeFetchRequest:request
 error:&error];

 修改记录
 player.score = [NSNumber numberWithInt:3500];
Core Data
 更多内容
 1. 版本升级 NSMappingModel
 2. NSManagedObjectContext与多线程
 3. NSManagedObject的生命周期
 4. iOS5.0, UIManagedDocument, iCloud
 5. NSFetchedResultsController与UITableView
 6. 内存使用与性能优化
 7. Cocoa Binding (Mac OS X)
数据持久化
 持久化方案的选择
  数据类型
  读写频率
  数据大小
  缓存机制
  安全性
  可扩展性
  开发效率
谢谢聆听!

Más contenido relacionado

La actualidad más candente

Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and DesignHo Kim
 
Script with engine
Script with engineScript with engine
Script with engineWebrebuild
 
jQuery源码学习
jQuery源码学习jQuery源码学习
jQuery源码学习fangdeng
 
Row Set初步学习V1.1
Row Set初步学习V1.1Row Set初步学习V1.1
Row Set初步学习V1.1Zianed Hou
 
冲浪 Object-c
冲浪 Object-c冲浪 Object-c
冲浪 Object-cjeff kit
 
Hibernate查询
Hibernate查询Hibernate查询
Hibernate查询llying
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈Robbin Fan
 
Sql基础培训
Sql基础培训Sql基础培训
Sql基础培训Ji ZHANG
 
17 Localization
17 Localization17 Localization
17 LocalizationTom Fan
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享晓锋 陈
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a databaseted-xu
 
Couchdb Beijing Openparty
Couchdb Beijing OpenpartyCouchdb Beijing Openparty
Couchdb Beijing Openpartylitaocheng
 
Apache Zookeeper 分布式服务框架
Apache Zookeeper 分布式服务框架Apache Zookeeper 分布式服务框架
Apache Zookeeper 分布式服务框架Cabin WJ
 
【Maclean liu技术分享】深入理解oracle中mutex的内部原理
【Maclean liu技术分享】深入理解oracle中mutex的内部原理【Maclean liu技术分享】深入理解oracle中mutex的内部原理
【Maclean liu技术分享】深入理解oracle中mutex的内部原理maclean liu
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starteryiming he
 
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo dbLucien Li
 

La actualidad más candente (19)

Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and Design
 
Script with engine
Script with engineScript with engine
Script with engine
 
Ooredis
OoredisOoredis
Ooredis
 
jQuery源码学习
jQuery源码学习jQuery源码学习
jQuery源码学习
 
Row Set初步学习V1.1
Row Set初步学习V1.1Row Set初步学习V1.1
Row Set初步学习V1.1
 
冲浪 Object-c
冲浪 Object-c冲浪 Object-c
冲浪 Object-c
 
Hibernate查询
Hibernate查询Hibernate查询
Hibernate查询
 
缓存技术浅谈
缓存技术浅谈缓存技术浅谈
缓存技术浅谈
 
MySQL入門介紹
MySQL入門介紹MySQL入門介紹
MySQL入門介紹
 
Sql基础培训
Sql基础培训Sql基础培训
Sql基础培训
 
17 Localization
17 Localization17 Localization
17 Localization
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
 
2, OCP - installing and creating a database
2, OCP - installing and creating a database2, OCP - installing and creating a database
2, OCP - installing and creating a database
 
MySQL進階介紹
MySQL進階介紹MySQL進階介紹
MySQL進階介紹
 
Couchdb Beijing Openparty
Couchdb Beijing OpenpartyCouchdb Beijing Openparty
Couchdb Beijing Openparty
 
Apache Zookeeper 分布式服务框架
Apache Zookeeper 分布式服务框架Apache Zookeeper 分布式服务框架
Apache Zookeeper 分布式服务框架
 
【Maclean liu技术分享】深入理解oracle中mutex的内部原理
【Maclean liu技术分享】深入理解oracle中mutex的内部原理【Maclean liu技术分享】深入理解oracle中mutex的内部原理
【Maclean liu技术分享】深入理解oracle中mutex的内部原理
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starter
 
深入学习Mongo db
深入学习Mongo db深入学习Mongo db
深入学习Mongo db
 

Destacado

Film noir narrative
Film noir narrativeFilm noir narrative
Film noir narrativethedavev28
 
Experience catalogue gastronomic center km0
Experience catalogue gastronomic center km0Experience catalogue gastronomic center km0
Experience catalogue gastronomic center km0Foodwine Tours
 
Corporate meetings and incentives in costa brava
Corporate meetings and incentives in costa bravaCorporate meetings and incentives in costa brava
Corporate meetings and incentives in costa bravaFoodwine Tours
 
Planning dilemma tms
Planning dilemma tmsPlanning dilemma tms
Planning dilemma tmsdfcioffi
 
Corporate meetings and incentives in penedès bio
Corporate meetings and incentives in penedès bioCorporate meetings and incentives in penedès bio
Corporate meetings and incentives in penedès bioFoodwine Tours
 
압구정미인피부시즌권
압구정미인피부시즌권압구정미인피부시즌권
압구정미인피부시즌권laehee4042
 
Experience catalogue individual and groups km0
Experience catalogue individual and groups km0Experience catalogue individual and groups km0
Experience catalogue individual and groups km0Foodwine Tours
 
Corporate meetings and incentives en wineries penedès-
Corporate meetings and incentives en wineries  penedès-Corporate meetings and incentives en wineries  penedès-
Corporate meetings and incentives en wineries penedès-Foodwine Tours
 
Scheduled Tours in Barcelona
Scheduled Tours in BarcelonaScheduled Tours in Barcelona
Scheduled Tours in BarcelonaFoodwine Tours
 
9 screen analysis
9 screen analysis 9 screen analysis
9 screen analysis thedavev28
 
Smart logistics and gps system
Smart logistics and gps systemSmart logistics and gps system
Smart logistics and gps systempirayavic
 
Facebook et les osbl les meilleures pratiques
Facebook et les osbl les meilleures pratiquesFacebook et les osbl les meilleures pratiques
Facebook et les osbl les meilleures pratiquestacticdirect
 
Enquête - Un an dans la vie des blogueurs
Enquête - Un an dans la vie des blogueurs Enquête - Un an dans la vie des blogueurs
Enquête - Un an dans la vie des blogueurs SLAP digital
 

Destacado (17)

Film noir narrative
Film noir narrativeFilm noir narrative
Film noir narrative
 
Cooking Classes
Cooking Classes Cooking Classes
Cooking Classes
 
Experience catalogue gastronomic center km0
Experience catalogue gastronomic center km0Experience catalogue gastronomic center km0
Experience catalogue gastronomic center km0
 
Corporate meetings and incentives in costa brava
Corporate meetings and incentives in costa bravaCorporate meetings and incentives in costa brava
Corporate meetings and incentives in costa brava
 
Planning dilemma tms
Planning dilemma tmsPlanning dilemma tms
Planning dilemma tms
 
GalerieFoto
GalerieFotoGalerieFoto
GalerieFoto
 
Tastings & Pairings
Tastings & Pairings Tastings & Pairings
Tastings & Pairings
 
Question 1
Question 1Question 1
Question 1
 
Corporate meetings and incentives in penedès bio
Corporate meetings and incentives in penedès bioCorporate meetings and incentives in penedès bio
Corporate meetings and incentives in penedès bio
 
압구정미인피부시즌권
압구정미인피부시즌권압구정미인피부시즌권
압구정미인피부시즌권
 
Experience catalogue individual and groups km0
Experience catalogue individual and groups km0Experience catalogue individual and groups km0
Experience catalogue individual and groups km0
 
Corporate meetings and incentives en wineries penedès-
Corporate meetings and incentives en wineries  penedès-Corporate meetings and incentives en wineries  penedès-
Corporate meetings and incentives en wineries penedès-
 
Scheduled Tours in Barcelona
Scheduled Tours in BarcelonaScheduled Tours in Barcelona
Scheduled Tours in Barcelona
 
9 screen analysis
9 screen analysis 9 screen analysis
9 screen analysis
 
Smart logistics and gps system
Smart logistics and gps systemSmart logistics and gps system
Smart logistics and gps system
 
Facebook et les osbl les meilleures pratiques
Facebook et les osbl les meilleures pratiquesFacebook et les osbl les meilleures pratiques
Facebook et les osbl les meilleures pratiques
 
Enquête - Un an dans la vie des blogueurs
Enquête - Un an dans la vie des blogueurs Enquête - Un an dans la vie des blogueurs
Enquête - Un an dans la vie des blogueurs
 

Similar a iOS程序设计-数据持久化

Android resource-management
Android resource-managementAndroid resource-management
Android resource-managementLucas Xu
 
Network and Multitasking
Network and MultitaskingNetwork and Multitasking
Network and Multitaskingyarshure Kong
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程Lixun Peng
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejsChi-wen Sun
 
Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流liuts
 
iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门Lucien Li
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门Lucien Li
 
Spring4.x + hibernate4.x_配置详解
Spring4.x + hibernate4.x_配置详解Spring4.x + hibernate4.x_配置详解
Spring4.x + hibernate4.x_配置详解zany_hui
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现hua qiu
 
mysql总结
mysql总结mysql总结
mysql总结haiwang
 
Flash基于对象的优化技术 黄炎中
Flash基于对象的优化技术 黄炎中Flash基于对象的优化技术 黄炎中
Flash基于对象的优化技术 黄炎中FLASH开发者交流会
 
03 Managing Memory with ARC
03 Managing Memory with ARC03 Managing Memory with ARC
03 Managing Memory with ARCTom Fan
 
Nosql及其主要产品简介
Nosql及其主要产品简介Nosql及其主要产品简介
Nosql及其主要产品简介振林 谭
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非Tony Deng
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享晓锋 陈
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoorobin yang
 
CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)Chengjen Lee
 

Similar a iOS程序设计-数据持久化 (20)

ios分享
ios分享ios分享
ios分享
 
Android resource-management
Android resource-managementAndroid resource-management
Android resource-management
 
Network and Multitasking
Network and MultitaskingNetwork and Multitasking
Network and Multitasking
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejs
 
Mongo db技术交流
Mongo db技术交流Mongo db技术交流
Mongo db技术交流
 
iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门iPhone,ios,Object-c基础入门
iPhone,ios,Object-c基础入门
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门
 
Spring4.x + hibernate4.x_配置详解
Spring4.x + hibernate4.x_配置详解Spring4.x + hibernate4.x_配置详解
Spring4.x + hibernate4.x_配置详解
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
 
mysql总结
mysql总结mysql总结
mysql总结
 
Flash基于对象的优化技术 黄炎中
Flash基于对象的优化技术 黄炎中Flash基于对象的优化技术 黄炎中
Flash基于对象的优化技术 黄炎中
 
03 Managing Memory with ARC
03 Managing Memory with ARC03 Managing Memory with ARC
03 Managing Memory with ARC
 
Nosql及其主要产品简介
Nosql及其主要产品简介Nosql及其主要产品简介
Nosql及其主要产品简介
 
Glider
GliderGlider
Glider
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
 
I os 08
I os 08I os 08
I os 08
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoo
 
CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)CKAN 技術介紹 (開發篇)
CKAN 技術介紹 (開發篇)
 

iOS程序设计-数据持久化

  • 1. iOS程序设计 -数据持久化 讲师: 谭奇宇 Email&MSN: qiyutan@hotmail.com
  • 2.
  • 4. 二进制文件  C语言函数库: fwrite, fread  iOS平台:NSFileManger, NSData
  • 5. 属性列表  定义 具有层次结构的对象列表,可以通过 NSPropertyListSerialization实现持久化。  存储格式 1. 标准XML 2. 二进制 3. 兹容旧格式(只读)
  • 7. 属性列表  写数据 NSMutableDictionary *info = [NSMutableDictionary dictionary]; [info setObject:@"Tom" forKey:@"Player"]; [info setObject:[NSNumber numberWithInt:3000] forKey:@"Score"]; [info writeToFile:fileName atomically:YES]; 或者 NSData *data = [NSPropertyListSerialization dataFromPropertyList:info format:NSPropertyListXMLFormat_v1_0 errorDescription:nil]; [data writeToFile:fileName atomically:YES]; 注意:NSDictionary的key对象必须实现NSCopying协议。若作为 属性列表,必须得是NSString,即使NSNumber都不可以, 因为key 值在XML中的元素类型只有1个<key>,无法区分类型。
  • 8. 属性列表 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Player</key> <string>Tom</string> <key>Score</key> <integer>3000</integer> </dict> </plist>
  • 9. 属性列表  读数据 NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:fileName]; 或者 NSData *data = [NSData dataWithContentsOfFile:fileName]; NSDictionary *info = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
  • 10. 属性列表 @interface Player : NSObject @property(retain) NSString *name; @property(assign) int score; @end Player *p = [[[Player alloc] init] autorelease]; p.name = @"Tom"; p.score = 3000; NSMutableDictionary *info = [NSMutableDictionary dictionary]; [info setObject:p forKey:@"Player"]; [info writeToFile:fileName atomically:YES]; //写操作失败
  • 11. NSUserDefaults  以plist格式存储<key, value>配置信息  存储位置 $HOME/Library/Preferences/<ApplicationBundleIdentifer>.plist
  • 12. NSUserDefaults  初始化配置信息 if(![NSStandardUserDefaults objectForKey:@"DefaultLevel"]){ [NSStandardUserDefaults setObject:[NSNumber numberWithInt:1] forKey:@"DefaultLevel"]; } … NSDictionary *defaultSettings = [NSDictionary dictionaryWithContentsOfFile:plistPathInBundle]; [NSStandardUserDefaults registerDefaults:defaultSettings];
  • 13. NSUserDefaults  不宜存放以下信息 1. 敏感数据 比如:帐号,密码 2. 大数据量信息  思考 如何保证程序的配置信息在越狱iOS上不会被恶意修改? 大数据量该如何存取?
  • 14. NSJSONSerialization  iOS 5.0版本引入,替代JSONKit等开源工具  将NSArray, NSDictionary转化成JSON数据格式  NSArray, NSDictionary中的元素必须是NSString, NSNumber, NSArray, NSDictionary, 或 NSNull  NSDictionary的key必须是NSString  用法 NSArray *players = …; [NSJSONSerialization dataWithJSONObject:players options:NSJSONWritingPrettyPrinted error:&error]; … players = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  • 16. 对象归档  NSCoder 1. 序列形式 NSArchiver, NSUnarchiver (iOS不支持) 2. Key-Value形式 NSKeyedArchiver, NSKeyedUnarchiver
  • 17. 对象归档  归档(Archive) NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:player forKey:@”Player"]; //… [archiver finishEncoding]; [data writeToFile:archivePath atomically:YES]; [NSKeyedArchiver archiveRootObject:player toFile:archivePath];
  • 18. 对象归档  解档(Unarchive) NSData *data = [NSData dataWithContentsOfFile:archivePath]; unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; player = [unarchiver decodeObjectForKey:@”player"]; [unarchiver finishDecoding]; [unarchiver release]; player = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
  • 19. 对象归档  NSCoding协议 @interface Player : NSObject<NSCoding> //… - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; //如果父类就已经实现了NSCoding协议 //self = [super initWithCoder:aDecoder]; if(self){ self.name = [aDecoder decodeObjectForKey:@"Name"]; self.score = [aDecoder decodeIntForKey:@"Score"]; } return self; }
  • 20. 对象归档  NSCoding协议 - (void)encodeWithCoder:(NSCoder *)aCoder { //[super encodeWithCoder]; [aCoder encodeObject:self.name forKey:@"Name"]; [aCoder encodeInt:self.score forKey:@"Score"]; }
  • 21. 对象归档  NSNumber, NSString, NSDate等,以及NSArray, NSDictionary, NSSet集合类均实现了NSCoding协议 NSMutableArray *players = [NSMutableArray array]; [players addObject:player1]; … //会调用数组中每个player对象的encoder [NSKeyedArchiver archiveRootObject:players toFile:archivePath]; //会调用decoder生成每个player对象 players = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
  • 22. SQLite  C语言接口,支持通用的SQL操作  无服务器  单文件  内存占用小,速度快,比较可靠  不适合存储多媒体数据:音视频、图片资源  并发处理方面比较弱(对手机端影响不大)
  • 23. SQLite  接口举例 1. 打开数据库 int sqlite3_open(constchar*filename,sqlite3**db); 1. 执行SQL语句 int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *context, char **error); 2. 读取查询结果 int mycallback(void*context,int count, char**values, char**cols); 3. 关闭数据库 int sqlite3_close(sqlite3 *db);
  • 24. Core Data  为何需要使用Core Data 1. 开发工作量和效率 2. 后期数据维护和升级 3. 性能优化 4. 线程安全 5. 更好地支持iCloud
  • 25. Core Data  数据管理框架,提供一套OC语法的接口  不是通常意义上的数据库,存储模型可以是SQLite, XML等
  • 26. Core Data  实体-关系模型 NSEntityDescription NSAttributeDescription NSRelationshipDescription NSManagedObjectModel
  • 27. Core Data  NSManagedObject-描述数据对象的类型 1. 数据对象均为NSManagedObject类型或派生类型 2. 利用Xcode可以自动生成表示Player, Team类代码
  • 28. Core Data  自动生成的Player类 @interface Player : NSManagedObject @property (nonatomic, retain) NSNumber * name; @property (nonatomic, retain) NSString * score; @property (nonatomic, retain) NSManagedObject *team; @end
  • 29. Core Data  自动生成的Player类 @interface Player : NSManagedObject @property (nonatomic, retain) NSNumber * name; @property (nonatomic, retain) NSString * score; @property (nonatomic, retain) NSManagedObject *team; @end @implementation Player //dynamic指示符用于告诉编译器运行时会生成对应的读取方法 @dynamic name; @dynamic score; @dynamic team; @end
  • 30. Core Data  自动生成的Team类 @class Player; @interface Team : NSManagedObject @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSSet *players; @end @interface Team (CoreDataGeneratedAccessors) //声明对应的添加和删除Player的方法,也是运行时才 实现 - (void)addPlayersObject:(Player *)value; @implementation Team - (void)removePlayersObject:(Player *)value; - (void)addPlayers:(NSSet *)values; @dynamic name; - (void)removePlayers:(NSSet *)values; @dynamic players; @end @end
  • 31. Core Data  NSManagedObject遵循KVC NSString *name = [managedObject valueForKey:@”name”]; 等价于 NSString *name = player.name;  什么情况下需要继承NSManagedContext? 1. 代码更直观简洁 2. 添加方法或属性 比如,可以增加portrait方法,根据player.name属性, 查询文件返回对应的UIImage对象
  • 32. Core Data  读取与存储数据 数据对象 NSManagedObject … NSManagedObject 数据对象管理器:增, NSManagedObjectContex 删,改,查,撤销/重 NSManagedObjectContext … t 做 数据字典(一般加载合并 Bundle中的所有model) NSPersistentStoreCoordinator NSManagedObjectMode l 数据文 NSPersistentStore … NSPersistentStore 件
  • 33. Core Data  初始化工作 model = [NSManagedObjectModel mergedModelFromBundles:nil]; //nil 表示从mainbundle中加载合并所有model coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]; context = [[NSManagedObjectContext alloc] init]; context.persistentStoreCoordinator = coordinator; 以上操作,iOS5.0下,等价于 UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:storeUrl];
  • 34. Core Data  增加记录 player = [NSEntityDescription insertNewObjectForEntityForName:@"Player” inManagedObjectContext:context]; team = [NSEntityDescription insertNewObjectForEntityForName:@"Team” inManagedObjectContext:context]; team.name = @"TheBest"; player.name = @"Tom"; player.score = [NSNumber numberWithInt:3000]; player.team = team; if(![context save:&error]){ NSLog(@"%@", error); }
  • 35. Core Data  删除记录 [context deleteObject:player];  查询记录 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Player"]; request.predicate = [NSPredicate predicateWithFormat:@"score > 2000"]; NSSet *players = [context executeFetchRequest:request error:&error];  修改记录 player.score = [NSNumber numberWithInt:3500];
  • 36. Core Data  更多内容 1. 版本升级 NSMappingModel 2. NSManagedObjectContext与多线程 3. NSManagedObject的生命周期 4. iOS5.0, UIManagedDocument, iCloud 5. NSFetchedResultsController与UITableView 6. 内存使用与性能优化 7. Cocoa Binding (Mac OS X)
  • 37. 数据持久化  持久化方案的选择  数据类型  读写频率  数据大小  缓存机制  安全性  可扩展性  开发效率