LFSCoreData是一个iOS和MacOX的开源库,帮助开发者轻松地开始基于Core Data框架的应用程序。轻松管理应用程序数据库的初始化、后台保存以及从API的映射。
要运行示例项目,请先克隆仓库,然后从项目目录运行pod install
。
在开始使用LFSCoreData之前,您应该阅读这些模式,并在xcodatamodel中创建实体。
NSDictionary *object = @{ name: @"Son", surname: @"Goku" };
User *user = [User importObject:object inContext:[[LFDataModel sharedModel] mainContext]];
// Save context to be persistent
[[LFDataModel sharedModel] saveContext];
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[Tweet importFromArray:JSON[@"results"] inContext:[[LFDataModel sharedModel]mainContext]];
// Save context to be persistent
[[LFDataModel sharedModel] saveContext];
[self updateTweetsInUI];
} failure:nil];
...
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[LFSaveInBackgroundOperation saveInBackgroundWithBlock:^(NSManagedObjectContext *backgroundContext) {
[Tweet importFromArray:JSON[@"results"] inContext:backgroundContext];
//Save in background already executes save method, so here is not necessary
} completion:^{
[self updateTweetsInUI];
}];
[self updateTweetsInUI];
} failure:nil];
…
...
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[LFSaveInBackgroundOperation saveInBackgroundWithBlock:^(NSManagedObjectContext *backgroundContext) {
Tweet *tweet = [Event objectForIdentifier:JSON[@"id"]
inManagedObjectContext:backgroundContext
forEntityName:[Tweet entityName]];
if (!tweet){
tweet = [Tweet insertInManagedObjectContext:backgroundContext];
}
tweet.tweetID = JSON[@"id"];
tweet.name = [JSON[@"name"] uppercaseString];
} completion:^{
[self updateTweetsInUI];
}];
[self updateTweetsInUI];
} failure:nil];
…
这样您可以从导入实体的数据库中删除除了正在导入的元素之外的所有元素。
当使用LFSCoreData作为数据缓存且这些数据易变时,很有用。
用法如下所示
[Tweet importFromArray:yourArrayOfNewObjects
inContext:[[LFSDataModel sharedModel] mainContext]
deleteOtherObjects:YES];
现在您可以通过这种方式删除特定实体的所有对象:
[Tweet deleteAllObjects];
LAFOSCA STUDIO S.L.
LFSCoreData开源协议为MIT。更多信息请参阅LICENSE文件。