iOS中经常使用的四种数据持久化方法简介

iOS中的数据持久化方式,基本上有如下四种:属性列表、对象归档、SQLite3和Core Datagit


1.属性列表
涉及到的主要类:NSUserDefaults,通常 [NSUserDefaults standardUserDefaults]就够用了github

@interface User : NSObject <NSCoding>
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
@end sql

使用方法
1).分开存取
// 存
[[NSUserDefaults standardUserDefaults] setInteger:userID forKey:@”userID”];
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@”name”];
// 取
NSInteger uId = [[[NSUserDefaults standardUserDefaults] integerValueForKey:@”userID”];
NSString* name = [[NSUserDefaults standardUserDefaults] stringForKey:@”name”];数据库


2).按对象存取
// 存
[[NSUserDefaults standardUserDefaults] setObject:self forKey:@”user”];
// 取
User* u = [[NSUserDefaults standardUserDefaults] objectForKey”@”user”];服务器

 

2.对象归档
要使用对象归档,对象必须实现NSCoding协议.大部分Object C对象都符合NSCoding协议,也能够在自定义对象中实现NSCoding协议,要实现NSCoding协议,实现两个方法:
- (void) encodeWithCoder:(NSCoder *)encoder 与 -(void)initWithCoder:(NSCoder *)encoder
同时,建议对象也同时实现NSCopying协议,该协议容许复制对象,要实现NSCopying协议须实现 -(id)copyWithZone:(NSZone *)zone 方法 。
@interface User : NSObject <NSCoding>
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
@end网络

@implementation User
// 如下两个方法必定要实现,否则在调用的时候会crash
- (void)encodeWithCoder:(NSCoder *)aCoder; 
{
// 这里放置须要持久化的属性
[aCoder encodeObject:[NSNumber numberWithInteger:self.userID] forKey:@”userID”];
[aCoder encodeObject:self.name forKey:@"name"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [self init])
{
//  这里务必和encodeWithCoder方法里面的内容一致,否则会读不到数据
self.userID = [[aDecoder decodeObjectForKey:@"userID"] integerValue];
self.name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}app

// 使用方法
+ (BOOL)save {
NSError *error = nil;
// 肯定存储路径,通常是Document目录下的文件
NSString* fileName = [self getFileName];
NSString* filePath = [self getFilePath];
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@”建立用户文件目录失败”);
return NO;
}
return [NSKeyedArchiver archiveRootObject:self toFile:[fileName:userId]];
}
@end编辑器

 

3.SQLite3
SQLite是一个开源的嵌入式关系数据库,它在2000年由D. Richard Hipp发布,它的减小应用程序管理数据的开销,SQLite可移植性好,很容易使用,很小,高效并且可靠。
SQLite嵌入到使用它的应用程序中,它们共用相同的进程空间,而不是单独的一个进程。从外部看,它并不像一个RDBMS,但在进程内部,它倒是完整的,自包含的数据库引擎。 嵌入式数据库的一大好处就是在你的程序内部不须要网络配置,也不须要管理。由于客户端和服务器在同一进程空间运行。SQLite 的数据库权限只依赖于文件系统,没有用户账户的概念。SQLite 有数据库级锁定,没有网络服务器。它须要的内存,其它开销很小,适合用于嵌入式设备。你须要作的仅仅是把它正确的编译到你的程序。
关于SQLite的开发资料较多,这里再也不细说。只是建议不直接操做SQLite库,而是采用一些开源的第三方库来进行操做。好比:
FMDB:https://github.com/ccgus/fmdb.git
对SQLite都作了不错的封装。atom


4.Core Data
Core Data本质上是使用SQLite保存数据,可是它不须要编写任何SQL语句。spa


要使用Core Data,须要在Xcode中的数据模型编辑器中设计好各个实体以及定义好他们的属性和关系。以后,经过操做这些对象,结合Core Data完成数据的持久化:


NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSString *fieldName = [NSString stringWithFormat:@"test%d", i];
UITextField *theField = [self valueForKey:fieldName];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//创 建描述语句,需求Line对象。相似于在数据库中限定为Line表。
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"  inManagedObjectContext:context];
[request setEntity:entityDescription];
//建立限制性语句,相似于SQL语句中的 where lineNum = i
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
[request setPredicate:pred];
NSManagedObject *theLine = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil){
NSLog(@”There was an error!”);
// Do whatever error handling is appropriate
}
if ([objects count] > 0){    //若是符合条件的object存在,则取出
theLine = [objects objectAtIndex:0];
}
else {  //若是不存在,则插入一个新的.
theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line"
inManagedObjectContext:context];
[theLine setValue:[NSNumber numberWithInt:i] forKey:@”lineNum”];  //设置这个object的属性,coredata会自动将其写入sqlite
[theLine setValue:theField.text forKey:@"lineText"];
[request release];

}


下面是其取数据的过程:

Core_Data_PersistenceAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];NSManagedObjectContext *context = [appDelegate managedObjectContext];NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"inManagedObjectContext:context];NSFetchRequest *request = [[NSFetchRequest alloc] init];[request setEntity:entityDescription];NSError *error;NSArray *objects = [context executeFetchRequest:request error:&error];if (objects == nil){NSLog(@”There was an error!”);// Do whatever error handling is appropriate}//每个对象在CoreData中都表示为一个NSManagedObject对象(相似于数据库表中的每一行),他的属性经过键/值 方式获取for (NSManagedObject *oneObject in objects){NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];NSString *lineText = [oneObject valueForKey:@"lineText"];}[request release];

相关文章
相关标签/搜索