撤消在主线程上执行的Core Data插入 [英] Undoing Core Data insertions that are performed off the main thread

查看:104
本文介绍了撤消在主线程上执行的Core Data插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些使用 NSOperation 导入数据的代码。我希望用户能够撤消在导入操作期间创建的 NSManagedObject 实例。

I'm working on some code that uses an NSOperation to import data. I'd like for the user to be able to undo the NSManagedObject instances that are created during the import operation.

从我可以知道,不可能使用 NSManagedObjectContext -undoManager 主线程。从使用线程限制来支持并发核心数据编程指南部分,我们有以下两个条件:

From what I can tell, it's impossible to use the NSManagedObjectContext -undoManager for any operations that are performed off of the main thread. From the Core Data Programming Guide section on Use Thread Confinement to Support Concurrency, we have these two conditions:



  1. 只有objectID应该在受管对象上下文之间传递
    (在
    单独的线程上)

  2. b $ b必须在
    之前的上下文中保存objectID可以使用。


这是有意义的,因为托管对象需要从私有存储( NSManagedObjectContext )移动到公共存储( NSPersistentStore

This makes sense since the managed objects need to be moved from private storage (NSManagedObjectContext) to public storage (NSPersistentStore) before they can be shared.

不幸的是, -save:消息还会导致撤消堆栈中的任何托管对象被移除。从同一指南的内存管理使用核心数据部分:

Unfortunately, the -save: message also causes any managed objects in the undo stack to be removed. From the Memory Management Using Core Data section of the same guide:


有待处理的管理对象
更改(插入,删除或
更新)由其上下文保存
,直到它们的上下文被发送save :,
reset,rollback或dealloc消息,

Managed objects that have pending changes (insertions, deletions, or updates) are retained by their context until their context is sent a save:, reset , rollback, or dealloc message, or the appropriate number of undos to undo the change.

我尝试过几个方法来解决这个限制,最终导致回到主线程(和旋转沙滩球)发生的大量工作。任何线索撤消工作与主线程创建的对象将非常感激。

I've tried several things to work around this limitation, and everything eventually leads back to bulk of the work happening on the main thread (and spinning beach balls.) Any clues to getting undo working with objects created off the main thread would be very much appreciated.

-

已提交增强型雷达: rdar://问题/ 8977725

推荐答案

这个答案可能会有点来回。如果我正确理解问题,您正在进行导入,但是当导入完成后,您希望用户能够选择导入中保存的内容?

This answer will probably be a bit of a back and forth. If I understand the issue correctly, you are doing an import but when the import is done you want the user to be able to select what gets saved from the import?

如果

如果是正确的,那么你可以做的是:

If it is correct then what you can do is:


  1. 将您的背景对象创建更改为

  1. Change your background object creation to

NSEntityDescription *myEntity = ... //Entity from your context
[[NSManagedObject alloc] initWithEntity:myEntity
         insertIntoManagedObjectContext:nil];


  • 将这些实体存储在数组中。


  • 调用 [myMainContext]

  • NSManagedObjectContext 执行保存。
  • / li>

  • Store these entities in an array.
  • Pass the entities back to your main thread as needed.
  • Release on any objects you don't want to keep
  • Call [myMainContext insertObject:managedObject] on any you want to keep.
  • Perform a save on the NSManagedObjectContext.
  • 由于这些实体不是 NSManagedObjectContext 的一部分,和应该是线程安全的,因为他们还没有绑定到 NSManagedObjectContext

    Since these entities are not part of a NSManagedObjectContext yet they only exist in memory and should be thread safe since they are not yet tied down to a NSManagedObjectContext.

    这当然是理论上的,需要测试。但它应该可以实现你的目标。

    This is of course theoretical and will require testing. However it should accomplish your goal.

    这篇关于撤消在主线程上执行的Core Data插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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