LFSCoreData 1.0.8

LFSCoreData 1.0.8

测试已测试
语言语言 Obj-CObjective C
许可证 自定义
发布日期最后发布日期2015年7月

David Cortés维护。



  • 作者
  • David Cortés

描述

LFSCoreData是一个iOS和MacOX的开源库,帮助开发者轻松地开始基于Core Data框架的应用程序。轻松管理应用程序数据库的初始化、后台保存以及从API的映射。

要运行示例项目,请先克隆仓库,然后从项目目录运行pod install

功能

  • 简单的设置和干净的AppDelegate
  • 从JSON数据轻松导入API数据
  • 自定义字段命名
  • 在主上下文中保存数据而不影响UI
  • 在NSOperationQueue后台保存

入门

设置

在开始使用LFSCoreData之前,您应该阅读这些模式,并在xcodatamodel中创建实体。

使用LFCoreDataPatterns设置项目

用法

导入一个对象

NSDictionary *object = @{ name: @"Son", surname: @"Goku" };
User *user = [User importObject:object inContext:[[LFDataModel sharedModel] mainContext]]; 

// Save context to be persistent
[[LFDataModel sharedModel] saveContext];

使用AFNetworking基本导入数组

...

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文件。