JSON到永久数据存储(CoreData等) [英] JSON to Persistent Data Store (CoreData, etc.)

查看:217
本文介绍了JSON到永久数据存储(CoreData等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式上的所有资料都是透过JSON透过API撷取。这个数据的好的百分比的性质是它不经常改变。所以去,并使JSON请求获得一个没有改变的数据列表似乎并不吸引人。

All of the data on my application is pulled through an API via JSON. The nature of a good percentage of this data is that it doesn't change very often. So to go and make JSON requests to get a list of data that doesn't change much didn't seem all that appealing.

我在寻找最明智的选择,让这个JSON保存在iPhone上的某种持久性数据存储。显然,当电话无法访问API时,会提供数据的一个加号。

I'm looking for the most sensible option to have this JSON saved onto the iPhone in some sort of persistent data store. Obviously one plus of persisting the data would be to provide it when the phone can't access the API.

我看过a少数 示例的JSON和CoreData交互,例如,但似乎他们只描述将NSManagedObjects转换为JSON。如果我可以将JSON转换为CoreData,我唯一的问题是当数据从API改变时,能够改变数据。

I've looked at a few examples of having JSON and CoreData interact, for example, but it seems that they only describe transforming NSManagedObjects into JSON. If I can transform JSON into CoreData, my only problem would be being able to change that data when the data from the API does change.

(或者,这可能是愚蠢的。)

(Or, maybe this is all just silly.)

推荐答案

对于某些概念阅读,您可能需要阅读有关有效导入数据,特别是查找或创建。 请参阅上一个类似问题。

For some conceptual reading, you may want to read documentation about Efficiently Importing Data, especially "Find-or-create". See a previous similar question.

获取JSON并将其作为Core Data保存在本地是非常合理的。我如何做到两个阶段:

Getting JSON and saving it as Core Data locally is highly reasonable. How I do it is in two stages:


  1. 将JSON转换为本机Cocoa数据类型(NSDictionary和NSArray)

  2. 将NS *转换为Core Data对象

在JSON和NS *之间转换的两个好框架是 json-framework TouchJSON 。下面的例子是基于json-framework的。

Two good frameworks for converting between JSON and NS* are json-framework and TouchJSON. The below example is based on json-framework.

假设你从JSON中获取一些对象的数组。然后您将:

Let's say you get an array of some objects from JSON. You'd then do:

NSArray *objects = [jsonStringYouGotFromServer JSONValue];
for (NSDictionary *_object in objects) {
    CDObjectType *cdObject = [self cdObjectFromDictionary:_object];
    // cdObject is now a full-featured Core Data object
}

cdObjectFromDictionary可能是这样的:

the cdObjectFromDictionary might be something like this:

- (CDObjectType *) cdObjectFromDictionary:(NSDictionary *)dict {

    CDObjectType *object = [NSEntityDescription
              insertNewObjectForEntityForName:@"Object"
              inManagedObjectContext:moc];

    NSDictionary *attributes = [[NSEntityDescription
        entityForName:@"Object"
        inManagedObjectContext:moc] attributesByName];

    for (NSString *attr in attributes) {            
        [object setValue:[dict valueForKey:attr] forKey:attr];                            
    }

    return object;
}

上述假设JSON和Core Data模型中的属性名称是和数据类型匹配(即JSON数字是核心数据NSNumbers等)。上述代码即使模型改变也能正常工作。

The above assumes that the attribute names in your JSON and Core Data model are the same and that the data types match (i.e JSON numbers are Core Data NSNumbers etc). The above code works nicely even if the model changes.

上面的代码不考虑如何测试对象是否已经存在于本地,但是你可以想象如何添加。您必须在模型中具有某种形式的对象ID,您可以在添加对象之前查看该对象是否本地存在,或者是否需要更新现有对象。

The above code does not consider how to test if the object already exists locally, but you can imagine how to add that. You must have an object ID of some form in your model, and you can see if the object exists locally before adding it, or whether existing object needs to be updated.

这篇关于JSON到永久数据存储(CoreData等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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