iOS同步 - 什么是Core Data对象知道它必须推送到服务器的好方法? [英] iOS synchronization - what is a good way for a Core Data object to know it has to be pushed to server?

查看:357
本文介绍了iOS同步 - 什么是Core Data对象知道它必须推送到服务器的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPad和网络服务器之间建立同步功能。我使用的方法非常接近此处描述的。我只有一种类型的对象,让我们称之为一个故事,必须同步。它是一个核心数据实体(管理对象)。

I am building sync functionality between an iPad and a web server. I'm using an approach pretty close to the one described here. I only have one type of object, let's called it a Story, that has to be synchronized. It is a Core Data entity (managed object).

我必须解决的剩余问题是知道每当有什么变化,需要同步到服务器。一种方法是去找到修改Story的每一段代码,并且还设置了一些needsSyncing标志。这似乎并不优雅,似乎随着时间的推移,开发人员可能会忘记更新标志的新类型的修改。

The remaining problem I have to solve is knowing "whenever something changes and needs to be synchronized to the server." One approach would be to go find every piece of code that modifies a Story and have it also set some needsSyncing flag. That does not seem elegant and it seems that over time, developers could forget to update the flag for new types of modification.

核心数据对象有一种方式来观察自己,所以任何时候进行任何更改,执行特定的方法?

Do Core Data objects have a way to observe themselves, so any time any change is made, a particular method is executed? That would make this pretty easy.

另一个选项可能是在对托管对象上下文执行保存操作之前使用isUpdated方法。你可能只能在一个地方保存调用,或者在你保存的每个地方都这样做(听起来像第一个选项)。我想我可以做一个帮助方法,通过所有的Story对象,在保存之前,看看他们是否需要他们的标志设置。唯一的缺点是,我将遍历系统中的所有Story对象进行任何保存,即使是与Story无关的保存。

Another option might be using the isUpdated method right before doing a save operation on the managed object context. You'd either have to have save called in only one place or do this at every place you save (sounds like the first option). I guess I could make a helper method that goes through all Story objects right before saving and see if any of them need their flag to be set. The only drawback to that is that I'd be traversing all Story objects in the system for any save, even for saves that have nothing to do with a Story.

反正我会停止尝试大声猜出解决方案 - 有没有人有一个很好的方法来做这个的经验?

Anyway I'll stop trying to guess the solution out loud - does anyone have experience with a good way to do this?

推荐答案

SDK你覆盖了。请参见 NSManagedObjectContext类参考,在页面的最后,MOC将发布您可以订阅的通知,包括NSManagedObjectContextObjectsDidChangeNotification。你可以听这些,并做更新调用几乎与保存MOC一致。

SDK has you covered. See the NSManagedObjectContext class reference, at the very end of the page, the MOC will post notifications that you can subscribe to, including NSManagedObjectContextObjectsDidChangeNotification. You can listen for these and do the update call pretty much coincident with saving the MOC.

这篇关于iOS同步 - 什么是Core Data对象知道它必须推送到服务器的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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