如何同步同步CoreData和REST Web服务,同时正确地将任何REST错误传播到UI中 [英] How to sync CoreData and a REST web service asynchronously and the same time properly propagate any REST errors into the UI

查看:133
本文介绍了如何同步同步CoreData和REST Web服务,同时正确地将任何REST错误传播到UI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我在这里为我们的应用程序的模型层工作。



一些要求是这样的:


  1. 它应该在iPhone OS 3.0 +上运行。

  2. 我们的数据源是RESTful Rails应用程序。

  3. 我们应该使用Core Data在本地缓存数据。

  4. 客户端代码(我们的UI控制器)应该尽可能不了解任何网络资源,
  5. 在WWDC10会话117中创建了 服务器驱动的用户体验,花了一些时间查看目标资源核心资源 RestfulCoreData 框架。



    目标资源框架本身不与Core Data交互,而只是一个REST客户端实现。核心资源和RestfulCoreData都假设你在你的代码中与Core Data交谈,他们解决了模型层上背景中的所有螺母和螺栓。



    远远和最初我虽然核心资源或RestfulCoreData将覆盖所有上述要求,但...有几件事情,他们似乎没有正确解决正确:


    1. 保存本地更新到服务器时,主线程不应被阻止。

    2. 如果保存操作失败,错误应传播到UI,应该保存到本地Core Data存储。

    当您调用<$时,Core Resource会向服务器发出所有请求c $ c> - (BOOL)save:(NSError **)error 在您的Managed Object上下文,因此能够提供一个正确的NSError实例的底层请求到服务器失败。但它阻塞调用线程,直到保存操作完成。 FAIL。



    RestfulCoreData会保持您的 -save:调用,并且不会为客户引入任何额外的等待时间线。它只是监视 NSManagedObjectContextDidSaveNotification ,然后在通知处理程序中向服务器发出相应的请求。但是这样, -save:调用总是成功完成(好吧,给定Core Data对保存的更改没关系),而实际调用它的客户端代码无法知道由于某些 404 421 或发生了任何服务器端错误,保存可能无法传播到服务器。甚至更多,本地存储变得更新数据,但服务器从不知道更改。 FAIL。



    所以,我在寻找一个可能的解决方案/常见的做法来处理所有这些问题:


    1. 我不希望调用线程在网络请求发生时阻塞每个 -save:调用。

    2. 我想以某种方式在UI中收到某些同步操作出错的通知。

    3. 如果服务器请求失败,我想要实际的Core Data保存失败。 li>

    任何想法?

    解决方案

    应该真正看看RestKit( http://restkit.org )这个用例。它旨在解决将远程JSON资源建模和同步到本地Core Data支持的缓存的问题。它支持离线模式,当没有网络可用时,完全从缓存工作。所有同步发生在后台线程(网络访问,有效负载解析和管理对象上下文合并),并且有一组丰富的委托方法,所以你可以知道发生了什么。


    Hey, I'm working on the model layer for our app here.

    Some of the requirements are like this:

    1. It should work on iPhone OS 3.0+.
    2. The source of our data is a RESTful Rails application.
    3. We should cache the data locally using Core Data.
    4. The client code (our UI controllers) should have as little knowledge about any network stuff as possible and should query/update the model with the Core Data API.

    I've checked out the WWDC10 Session 117 on Building a Server-driven User Experience, spent some time checking out the Objective Resource, Core Resource, and RestfulCoreData frameworks.

    The Objective Resource framework doesn't talk to Core Data on its own and is merely a REST client implementation. The Core Resource and RestfulCoreData all assume you talk to Core Data in your code and they solve all the nuts and bolts in the background on the model layer.

    All looks okay so far and initially I though either Core Resource or RestfulCoreData will cover all of the above requirements, but... There's a couple of things none of them seemingly happen to solve correctly:

    1. The main thread should not be blocked while saving local updates to the server.
    2. If the saving operation fails the error should be propagated to the UI and no changes should be saved to the local Core Data storage.

    Core Resource happens to issue all of its requests to the server when you call - (BOOL)save:(NSError **)error on your Managed Object Context and therefore is able to provide a correct NSError instance of the underlying requests to the server fail somehow. But it blocks the calling thread until the save operation finishes. FAIL.

    RestfulCoreData keeps your -save: calls intact and doesn't introduce any additional waiting time for the client thread. It merely watches out for the NSManagedObjectContextDidSaveNotification and then issues the corresponding requests to the server in the notification handler. But this way the -save: call always completes successfully (well, given Core Data is okay with the saved changes) and the client code that actually called it has no way to know the save might have failed to propagate to the server because of some 404 or 421 or whatever server-side error occurred. And even more, the local storage becomes to have the data updated, but the server never knows about the changes. FAIL.

    So, I'm looking for a possible solution / common practices in dealing with all these problems:

    1. I don't want the calling thread to block on each -save: call while the network requests happen.
    2. I want to somehow get notifications in the UI that some sync operation went wrong.
    3. I want the actual Core Data save fail as well if the server requests fail.

    Any ideas?

    解决方案

    You should really take a look at RestKit (http://restkit.org) for this use case. It is designed to solve the problems of modeling and syncing remote JSON resources to a local Core Data backed cache. It supports an offline mode for working entirely from the cache when there is no network available. All syncing occurs on a background thread (network access, payload parsing, and managed object context merging) and there is a rich set of delegate methods so you can tell what is going on.

    这篇关于如何同步同步CoreData和REST Web服务,同时正确地将任何REST错误传播到UI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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