为MVC和KVO设计帮助(Design assistance for MVC and KVO)

编程入门 行业动态 更新时间:2024-10-27 23:30:18
为MVC和KVO设计帮助(Design assistance for MVC and KVO)

我的设备上存在需要与我的服务器同步的数据,在设计方面我是否在正确的轨道上?

我有一个模型,有一个返回我的本地对象的方法,让我们称之为[model objects]';

在这个方法中,它立即返回本地对象,但我也想检查服务器是否需要更新任何这些对象,所以在后台线程中运行一个方法,该方法使用async NSUrlConnection ,它将返回每个对象的JSON。需要更新,我将其保存到核心数据。 在表视图控制器中,我将设置一个KVO来观察何时更改了updatedObjects的值,然后重新加载updatedObjects显示的对象。

问题:这是处理此问题的正确方法吗? 我错过了什么吗? 我能以某种方式改进吗?

我也在考虑禁用正在更新的对象的UITableViewCells,并在下载和保存阶段显示UIActivityIndicator ,但我不确定这是否会导致UX中的任何竞争条件。

希望这个解释得足以让您理解,如果您有任何问题,我会立即尝试回复。

I have data that is persisted on the device that needs to sync with my my server, in terms of design am I on the right track?

I have a model with a method to return my local objects, let's call it [model objects]';

Within this method it returns the local objects immediately but I'd also like to check if the server needs to update any of these objects so in the background thread I run a method that uses an async NSUrlConnection that will return the JSON of each object that needs to be updated in which I will save that to core data. In the Table View Controller I would set a KVO to observe when the values changed for the updatedObjects then reload the objects displayed in the objectTableView.

Question: Is this the correct way to handle this? Am I missing something? Can I improve this somehow?

I am also thinking of disabling the UITableViewCells of the objects that are being updated and showing a UIActivityIndicator during the downloading and saving stages but I'm not sure if this will cause any race conditions in the UX.

Hopefully this was explained well enough for you to understand, if you have any questions I will try to reply immediately.

最满意答案

CoreData中存在对此类问题的支持。 并且有各种方法。

基本要点是,您在后台线程中为异步Web调用创建新上下文,然后将本地数据与管理更新的新接收数据进行比较。 然后执行从线程上下文到主上下文的合并。 此时,您可以在CoreData中接收更新数据的通知并更新您的视图。

您甚至可以使用NSFetchedResultsController来进一步简化这一过程。

There is support for this sort of problem within CoreData. And there are a variety of approaches.

The basic gist is that you create a new context in your background thread for your async web call, then comparing your local data to the new received data you manage updates. Then you perform a merge from your thread context to your main context. At this point you can receive notification of the updated data in CoreData and update your views.

You can even use NSFetchedResultsController to simplify this even more.

更多推荐

本文发布于:2023-07-08 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:KVO   MVC   assistance   Design

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!