存储在Web服务的核心数据中

时间:2010-06-22 13:15:48

标签: iphone web-services core-data

你能不能给我一个例子来解释将数据存储在从webservice收到的coredata中的过程。我不想使用sqlite DB。也许是一个展示这种例子的链接。

1 个答案:

答案 0 :(得分:4)

特别是没有任何与Web服务相关的特殊技巧。从服务获取数据后,您将创建Core Data类的实例,然后使用数据填充它们,就像使用来自任何其他源(如UI)的数据一样。

例如,大多数JSON实现提供的返回数据是字典。您只需将核心数据对象中的属性设置为字典中的相应值即可。

如果您不熟悉如何设置和使用核心数据,您应该先了解它。

编辑:

来自评论:

  好的,好的。所以我明白如何分配一个   文件的位置(如果是)   文件夹。但在这种情况下我   我从网络服务获取数据   这是以一种形式收到的   响应。我在这种情况下做什么?   将无法分配位置   商店

数据源与商店文件的位置之间没有任何关联。在正常情况下,您将不会连接到Web服务器上的Core Data托管商店(可能但很少见。)而是来自服务器的数据是标准格式,如JSON,REST等。您的应用程序将创建本地存储(无论您决定放置它)并初始化Core Data堆栈。然后它将获取JSON解析器的输出并创建NSManagedObjects来表示数据。然后它将这些对象保存到商店,就像将数据形成任何其他来源一样。

因此,在伪代码中它看起来像:

Core Data{
    Create local persistent store in desired location;
    Create managed object context;
    Load managed object model:
} 

Network Inteface {
    Connect to Server;
    Send request;
    Parse request into objective-C data structure (array, dictionary, etc)
}   

Load Data strucutre into Core Data{
    Either {
        Insert into context NSManageObject for entity that models recieved data
        or
        Insert into context instance of NSManagedObject subclass that models the recieved data
    }
    Set attributes of entity/instance to the appriopiate fields in the recieved data structure. 
    Save managed object context (which saves the entity/instance to the file on disk)   
}

我认为你认为这是一个比它更复杂和更复杂的过程。它基本上与将数据从服务器保存到本地文本文件没有什么不同。唯一的区别是您要保存到Core Data堆栈。

相关问题