1

So I am implementing a feeds/timeline kind of app. Where basically when you open up the app initially it is going to memory and then use that data to return immediately, while in the background it is fetching the newer data from the server. The issue is that as of now I am storing the response to disk as raw JSON file. Now when I get back the new data from the server as JSON, basically I need to append/merge this JSON with the old one before saving it back to disk. This is quite a pain as I don't know how to merge the two JSON? Is it possible, if yes how?

The JSON format is as follows:

{
    "data": {
        "last_updated": 1342277036, 
        "stream": [
            {
                "action": {

                    }
                }, 
                "to": [


                ], 
                "from": [

                ],  
                "timestamp": 1342276421
            }, 

 {
                "action": {

                    }
                }, 
                "to": [


                ], 
                "from": [

                ],  
                "timestamp": 1342276421
            }, 

 {
                "action": {

                    }
                }, 
                "to": [


                ], 
                "from": [

                ],  
                "timestamp": 1342276421
            }, 

If this is not a good way to do it. Then is is better to save the NSObject's to disk instead of the raw JSON file?

4

1 回答 1

0

您应该使用自己的 ObjC 对象对数据进行建模,而不是合并 JSON 对象。

查看您的 JSON 数据,您的流似乎包含“动作”、到/从和时间。

因此,您应该创建一个代表该数据的类。

然后,当您下载 JSON 时,您应该解析它并根据收到的数据创建操作对象。

然后,您可以使用诸如 Core Data 之类的框架来处理您的持久性。Core Data 是一个对象模型图,可以有数据库支持,你可以使用 Core Data 来查询数据库。

这将允许您执行诸如“获取自此时间戳以来的所有操作对象”和“将所有这些新对象保存到数据库”之类的操作。

查看Core Data 编程指南

于 2012-07-14T15:34:18.243 回答