将Web服务数据缓存到核心数据中 [英] Cache Webservice data into core data

查看:67
本文介绍了将Web服务数据缓存到核心数据中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Web服务数据缓存到核心数据的好方法是什么?主要目的是在设备在线或将发生某些更新并且用户也能够离线获取数据时将Web服务数据同步到核心数据因此,将如何实施该建议并提出任何好的建议。

What's the good way to accompolish caching of web service data into core data.Main objective is to sync web service data into core data when device will be online or some update will be happened and user able to get the data in offline also.So how that thing will be implemented any good suggestions.

推荐答案

我已经做到了。我的过程是使用网络客户端(在我的情况下为AFNetworking),然后在成功请求后执行以下操作:

I have done this. My process was to use a network client (in my case, AFNetworking), and then upon a successful request, I would do the following:

从Core删除所有对象数据实体

Remove all objects from the Core Data Entity

在AppDelegate中创建一个NSOperationQueue并使用一个自定义的Parser类,该类实例化了一个新的ManagedObjectContext来在后台线程上使用

Create an NSOperationQueue in the AppDelegate and use a custom Parser class that instantiated a new ManagedObjectContext to be used on the background thread

解析来自Web服务的响应,然后使用

Parse the response from the web service and insert the objects back into the CD Entity on a background thread using

// Register context with the notification center
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
[nc addObserver:self
       selector:@selector(mergeChanges:) 
           name:NSManagedObjectContextDidSaveNotification
          object:ctx];

然后,当我保存托管对象(每五遍保存一次)时,通知将得到

Then when I saved the managed object (I did a save every five passes), the notification would get sent and this method would get fired:

- (void)mergeChanges:(NSNotification *)notification
{
    id appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];

    // Merge changes into the main context on the main thread
    [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
                              withObject:notification
                           waitUntilDone:NO];
}

这将主要在后台更新Core Data Entity,然后合并更改(每五个记录)在主线程中。我为用户提供的最初UI是一个TableView,它依赖于要更新的​​核心数据实体,并且更新速度足够快,用户可以在传入新数据时使用tableview(FetchedResultsController可以管理新数据的插入电视中的行)。

This would update the Core Data Entity mostly in the background and then merge the changes (every five "records") in the main thread. My initial UI for the user was a TableView that relied on the Core Data Entity being updated and the update was plenty fast enough for the user to be able to use the tableview while new data was coming in (the FetchedResultsController would manage the inserting of new rows in the TV).

我可以根据需要发送更多代码,但要点是使用NSOperationQueue在后台线程上解析创建的托管对象,然后使用主线程的MOC经常(在我的情况下为5条记录)合并对上下文的更改。

I can send more code if needed, but the gist of it was to parse the created the managed objects on a background thread using NSOperationQueue and then merge the changes to the context every so often (in my case 5 records) using the main thread's MOC.

这篇关于将Web服务数据缓存到核心数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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