您的位置:首页 > 移动开发 > IOS开发

iOS中 数据持久化方式

2015-10-09 09:14 411 查看
iOS中的数据持久化方式,基本上有以下四种:

 属性列表、对象归档、SQLite3和Core Data

1.属性列表

属性列表文件是一种XML文件,Foundation框架中的数组和字典等都可以于属性列表文件相互转换。

NSArray类常用读写属性列表文件的方法

+arrayWithContentsOfFile:类级构造方法,用于从属性列表文件中读取数据,创建NSArray对象。

-initWithContentsOfFile:实例构造方法,用于从属性列表文件中读取数据,创建NSArray对象。

-writeToFile:atomically:该方法把NSArray对象写入到属性列表文件中,第一个参数是文件名,第二个参数为是否使用辅助文件,如果为YES,则先写入到一个辅助文件,然后辅助文件再重新命名为目标文件,如果为NO,则直接写入到目标文件。

NSDictionary类常用读写属性列表文件的方法:

+dictionaryWithContentsOfFile:类级构造方法,用于从属性列表文件中读取数据,创建NSDictionary对象。

-initWithContentsOfFile:实例构造方法,用于从属性列表文件中读取数据,创建NSDictionary对象。

-writeToFile:atomically:该方法将NSDictionary对象写入到属性列表文件中。

属性列表文件数据持久化具体方法,可参考以下实现方式:

假如在项目中手工创建了一个Contacts.plist文件,并在该文件中添加了几条数据,如下图所示。

当然也可以通过代码直接创建plist文件。



接下来需要做的是将项目资源的Contacts.plist文件中数据复制到沙箱Documents目录下。

//对文件进行预处理,判断在Documents目录下是否存在plist文件,如果不存在则从资源目录下复制一个。
-(void)createEditableCopyOfDatabaseIfNeeded
{
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *writableDBPath=[self applicationDocumentsDirectoryFile];

BOOL dbexits=[fileManager fileExistsAtPath:writableDBPath];
if (!dbexits) {
NSString *defaultDBPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Contacts.plist"];

NSError *error;
BOOL success=[fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

if (!success) {
NSAssert1(0,@"错误写入文件:‘%@’",[error localizedDescription]);
}
}
}

//获取放置在沙箱Documents目录下的文件的完整路径
-(NSString *)applicationDocumentsDirectoryFile
{
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path=[documentDirectory stringByAppendingPathComponent:@"Contacts.plist"];

return path;
}


createEditableCopyOfDatabaseIfNeeded方法中
NSFileManager的copyItemAtPath:toPath:error:方法实现文件复制。
NSAssert1是Foundation框架提供的宏,它在断言失败的情况下抛出异常,类似的还有NSAssert和NSAssert2等。
applicationDocumentsDirectoryFile方法中
stringByAppendingPathComponent:能够在目录后面追加文件名,返回完整的文件路径。
沙箱Documents目录下成功生成plist文件之后,就可以进行增、删、改、查操作了。可参考如下代码:

NSString *path=[self applicationDocumentsDirectoryFile];

//将属性列表文件内容读取到array变量中,也就是获取了属性列表文件中全部的数据集合
NSMutableArray *array=[[NSMutableArray alloc]initWithContentsOfFile:path];

//向array中添加一条新记录
NSDictionary *newContact=[NSDictionary dictionaryWithObjects:@[contact.Title,contact.Type] forKeys:@[@"Title",@"Type"]];
[array addObject:newContact];

//删除array中一条记录
[array removeObjectAtIndex:0];

//删除array中全部记录
[array removeAllObjects];

for (NSDictionary* dict in array) {
//通过for循环,找到需要修改的数据项,进行修改数据
[dict setValue:@"Test" forKey:@"Title"];
}

//将array重新写入属性列表文件中
[array writeToFile:path atomically:YES];




涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults​ standardUserDefaults​]就够用了

@interface User : NSObject <NSCoding>

@property (nonatomic, assign) NSInteger userID;

@property (nonatomic
4000
, copy) NSString *name;

@end

使用方法

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;

}

// 使用方法

+ (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都做了不错的封装。


4.​Core Data

Core Data本质上是使用SQLite保存数据,但是它不需要编写任何SQL语句。

要使用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];​
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: