问题描述
我需要保存更改不仅局部到核心数据
,但在服务器上。
I need to save changes not only locally into Core Data
, but on server too.
我担心的是,在我的情况下,用户可以在短时间内完成大量的互动。在交互之间没有足够的时间来接收从服务器返回的成功消息。所以要么我锁定GUI,直到下一条消息返回 - 这是现在的情况 - ,或选择一种不同的方法。
My concern is, in my case user can do bunch of interaction in a short time. Between interaction there is not enough time to receive success message returned from server. So either I lock the GUI, until next message returns - this is the case now -, or choose a different approach.
我的新方法是让用户做许多交互并将事务放在 undo stack
$ c> NSUndoManager ,在 NSManagedObjectContext
,启用,仅保存/提交成功消息的事务。如何移动撤消光标一次一个,提交记录一个一个,虽然上下文包含已经植根的未保存的更改?
My new approach would be to let user do many interactions and put transactions onto undo stack
provided by NSUndoManager
, enabled on NSManagedObjectContext
, BUT save / commit ONLY that transaction for which success message was received. How can I move undo "cursor" one at a time, commit records one by one, although context contains already planty of unsaved changes?
推荐答案
NSUndoManager
不是真的适合这个任务。您可以让它撤消或重做动作,但您无法检查这些动作或选择性地将数据保存在当前的撤消堆栈中。
NSUndoManager
is not really suited to this task. You can tell it to undo or redo actions, but you can't inspect those actions or selectively save data in the current undo stack.
我在过去是创建我自己的传出更改的队列。每当在本地保存更改时,请将这些更改添加到未同步的出局更改列表。然后使用不同的队列通过将队列发送到服务器来处理队列,如果服务器报告成功,则清除这些更改。您可以使用 NSManagedObjectContextWillSaveNotification
和/或 NSManagedObjectContextDidSaveNotification
来监视更改和更新出站队列。
What I've done in the past is create my own queue of outgoing changes. Whenever changes are saved locally, add those changes to a list of un-synced outgoing changes. Then use a different queue to handle processing that queue by sending them to the server and, if the server reports success, clearing out those changes. You can use NSManagedObjectContextWillSaveNotification
and/or NSManagedObjectContextDidSaveNotification
to monitor changes and update the outbound queue.
这意味着iOS设备可能有排队的更改,服务器不知道,特别是如果网络不可靠或不可用。这在那些情况下几乎是不可避免的,除非你做一些可怕的东西,如拒绝让人们做新的更改,直到网络恢复。
This means that the iOS device may have queued changes that the server doesn't know about, especially if the network is unreliable or unavailable. That's pretty much unavoidable in those situations though, unless you do something awful like refuse to let people make new changes until the network comes back up.
这篇关于保存NSUndoManager事务一个接一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!