gpt4 book ai didi

ios - CoreData 删除上下文中的对象

转载 作者:行者123 更新时间:2023-11-28 07:49:25 25 4
gpt4 key购买 nike

问题:

  • 删除 NSManagedObject 是否必须在 context.perform/context.performAndWait block 中完成?
  • 或者删除 block 外的对象是否安全?

代码:

func delete(something: NSManagedObject, context: NSManagedObjectContext) {

context.performAndWait { //Is context.perform / context.performAndWait required to delete an object ?

context.delete(something)
}
}

我的想法:

  • 由于此代码是从不同的线程(后台/主线程)调用的,因此最好使用 context.perform/context.performAndWait
  • 上下文可能是用特定的并发类型(主/私有(private)队列)创建的。
  • 上下文的并发类型需要与执行代码的线程(主/后台)相匹配。
  • 即使具有不同不匹配线程类型的线程正在执行该 block ,该 block 也会确保它正常运行。

最佳答案

根据我个人的经验,使用performAndWait,因为它会等到操作完成。无论如何,这两种方法都将在它自己的线程上运行。(上下文的线程)。

From Documentation:

perform(:) and performAndWait(:) ensure the block operations are executed on the queue specified for the context. The perform(:) method returns immediately and the context executes the block methods on its own thread. With the performAndWait(:) method, the context still executes the block methods on its own thread, but the method doesn’t return until the block is executed.

关于ios - CoreData 删除上下文中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50095197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com