什么是同步Core Data与JSON API的最有效的方法 [英] What is the most efficient way to sync Core Data with JSON API

查看:178
本文介绍了什么是同步Core Data与JSON API的最有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Core Data模式与远程API同步服务JSON的最佳方式是什么?目前,我在JSON响应中检查Core Data中的每个字典,以查看API ID是否存在。

What would be the best way to sync my Core Data schema with a remote API serving JSON? At the moment I'm looping through each dictionary in the JSON response checking Core Data to see if the API ID exists.

这样做效果很好,但现在要做的所有工作都是删除服务器上没有的任何本地对象。以下是我的JSON数据示例:

This works great, but all thats left to do now is delete any local objects that aren't on the server. Here is an example of my JSON data:

[
   {
      "id":1234,
      "name":"My first object",
      "description":"This is a long string with lots of information"
   },
   {
      "id":1235,
      "name":"My second object",
      "description":"This is a long string with lots of information"
   }
]

目前我可以想到的唯一方法是完成这个操作,如下所示:

Currently the only way I can think of accomplishing this is something like the following:

NSArray *jsonData = // Convert json data to array using NSJSONSerialization
NSInteger fetchedCount = _fetchedResultsController.fetchedObjects.count;

if (fetchedCount != jsonData.count) {

    for (int i = 0; i < fetchedCount; i++) {

        NSManagedObject *object = [_fetchedResultsController objectAtIndexPath: [NSIndexPath indexPathForItem:i
                                                                                                    inSection:0]];
        NSNumber *idNumber = object.apiID;

        BOOL shouldDelete = YES;

        for (NSDictionary *jsonDict in jsonData) {
            if ([jsonDict  objectForKey:@"id"] == idNumber) {
                shouldDelete = NO;
            }
        }

        if (shouldDelete) {

            // Delete object.
        }            
    }
}

推荐答案

这可以确定,但我认为你应该应用 >在Apple doc中建议的查找或创建模式。请参阅这里深入说明有效导入数据(在具体请参阅实施有效地查找或创建)。

This could be ok, but I think you should apply the Find-or-Create pattern suggested in Apple doc. See here for a deep explanation Efficiently Importing Data (In particular see Implementing Find-or-Create Efficiently).

总体思路很简单。有两个对象数组(您从Core Data中检索的对象和从服务检索的对象),它们是排序的(通过 apiID id )。

The overall idea is quite simple. Having two arrays of objects (the one you retrieve from Core Data and the one you retrieve from the service) that are ordered (by apiID and id resp.).

显然,如果有很多数据,我真的建议在后台执行操作。记住每个线程需要依赖它的 NSManagedObjectContext 。否则,利用iOS 5 API提供的新队列机制。

Obviously if there are a lot of data, I really suggest to perform operations in background. Remember that each thread needs to rely on its NSManagedObjectContext. Otherwise take advantage of new queue mechanism provided by iOS 5 API.

为了完整起见,我还建议阅读RayWenderlich教程如何将核心数据与Web服务同步第1部分和第2部分非常有趣。

For the sake of completeness, I also suggest to read RayWenderlich tutorial How To Synchronize Core Data with a Web Service Part 1 and 2. It's very interesting.

希望有帮助。

这篇关于什么是同步Core Data与JSON API的最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆