在不访问主线程的情况下保存 NSManagedObjectContext
Saving NSManagedObjectContext without hitting the main thread
我想做什么:
- 在不冻结 UI 的情况下与网络 API 执行后台同步。我正在使用 MagicalRecord 但它并不是真正特定于它的。
- 确保我正确使用上下文等
我真正的问题是:我的理解正确吗?最后还有几个问题。
因此,MagicalRecord 提供的上下文是:
- MR_rootSavingContext of PrivateQueueConcurrencyType 用于将数据持久化到存储,这是一个缓慢的过程
- MR_defaultContext MainQueueConcurrencyType
- 对于背景,您可能希望使用由 MR_context() 生成的上下文,它是 child MR_defaultContext 并且属于 PrivateQueueConcurrencyType
现在,为了以异步方式保存,我们有两个选择:
- MR_saveToPersistentStoreWithCompletion() 这将一直保存到 MR_rootSavingContext 并写入磁盘
- MR_saveOnlySelfWithCompletion() 最多只能保存到 parent 上下文(即 MR_defaultContext 用于创建的上下文MR_context)
从那里开始,我认为我可以在不冻结 UI:
的情况下执行以下操作(我们称之为尝试#1)
let context = NSManagedObjectContext.MR_context()
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
context.MR_saveOnlySelfWithCompletion(nil)
}
// I would normally call MR_saveOnlySelfWithCompletion here, but calling it inside the loop makes any UI block easier to spot
但是,我的假设是错误的。我looked into MR_saveOnlySelfWithCompletion 看到它依赖于
[self performBlock:saveBlock];
Asynchronously performs a given block on the receiver’s queue.
所以我有点困惑,因为我希望它不会因此而阻塞 UI。
然后我尝试了(我们称之为尝试#2)
let context = NSManagedObjectContext.MR_context()
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
context.MR_saveOnlySelfWithCompletion(nil)
}
}
这样就可以了,但感觉不对。
中发现了一些东西
When sending messages to a context created with a queue
association, you must use the performBlock: or performBlockAndWait:
method if your code is not already executing on that queue (for the
main queue type) or within the scope of a performBlock... invocation
(for the private queue type). Within the blocks passed to those
methods, you can use the methods of NSManagedObjectContext freely.
所以,我假设:
- Attempt#1 冻结 UI 因为我实际上是从主队列调用它而不是在 performBlock
的范围内
- 尝试 #2 有效,但我正在创建另一个线程,而上下文已经有自己的后台线程
所以of course我应该做的是使用saveWithBlock:
MagicalRecord.saveWithBlock { (localContext) -> Void in
for i in 1...1_000 {
User.MR_createInContext(context)
}
}
这将执行属于 PrivateQueueConcurrencyType 的操作 on a direct child of MR_rootSavingContext。
感谢 rootContextChanged,MR_rootSavingContext 之前的任何更改都将适用于 MR_defaultContext。
这么看来:
- MR_defaultContext 是显示数据的完美上下文
- 编辑最好在 MR_context(child of MR_defaultContext)
中完成
- long 运行 诸如服务器同步之类的任务最好使用 saveWithBlock
完成
它仍然没有得到的是如何使用 MR_save[…]WithCompletion()。我会在 MR_context 上使用它,但由于它在我的测试用例中阻塞了主线程,所以我看不到它何时变得相关(或者我错过了什么……)。
感谢您的宝贵时间:)
好的,我很少使用魔法记录,但既然你说你的问题比较笼统,我会尝试回答。
一些理论:创建上下文时,您会传递一个指示器,指示您是希望它绑定在主线程还是后台线程上
let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
"bound" 是指线程在内部由上下文引用。在上面的示例中,上下文创建并拥有一个新线程。此线程 不会自动使用,但必须显式调用 为:
context.performBlock({ () -> Void in
context.save(nil)
return
});
所以你的代码 'dispatch_async' 是错误的,因为上下文绑定到的线程只能从上下文本身引用(它是一个私有线程)。
从上面你要推断的是,如果上下文绑定到主线程,从主线程调用performBlock将不会 做任何与直接调用上下文方法不同的事情。
最后评论你的要点:
MR_defaultContext 是显示数据的完美上下文:NSManagedObject 必须从它所在的上下文访问
创建后,它实际上是 only 上下文,您可以为
UI 来自.
编辑最好在 MR_context(child of MR_defaultContext)中完成:编辑并不昂贵而且你应该跟着
上面的规则。如果您正在调用一个从主线程编辑 NSManagedObject 属性的函数(比如点击按钮)
您应该更新主要上下文。另一方面,保存是
昂贵,这就是为什么您的主要上下文不应链接到
直接持久存储,但只是将其编辑下推到根目录
具有持久存储的后台并发上下文。
long 运行 诸如服务器同步之类的任务最好使用 saveWithBlock 完成。
现在,尝试 1
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
}
context.MR_saveOnlySelfWithCompletion(nil)
无需为每个 object 创建都保存。即使 UI 没有被阻止也是浪费。
关于 MR_context。在魔法记录的文档中我看不到 'MR_context' 所以我想知道这是否是访问主要上下文的快速方法。如果是这样,它会阻塞。
我想做什么:
- 在不冻结 UI 的情况下与网络 API 执行后台同步。我正在使用 MagicalRecord 但它并不是真正特定于它的。
- 确保我正确使用上下文等
我真正的问题是:我的理解正确吗?最后还有几个问题。
因此,MagicalRecord 提供的上下文是:
- MR_rootSavingContext of PrivateQueueConcurrencyType 用于将数据持久化到存储,这是一个缓慢的过程
- MR_defaultContext MainQueueConcurrencyType
- 对于背景,您可能希望使用由 MR_context() 生成的上下文,它是 child MR_defaultContext 并且属于 PrivateQueueConcurrencyType
现在,为了以异步方式保存,我们有两个选择:
- MR_saveToPersistentStoreWithCompletion() 这将一直保存到 MR_rootSavingContext 并写入磁盘
- MR_saveOnlySelfWithCompletion() 最多只能保存到 parent 上下文(即 MR_defaultContext 用于创建的上下文MR_context)
从那里开始,我认为我可以在不冻结 UI:
的情况下执行以下操作(我们称之为尝试#1)let context = NSManagedObjectContext.MR_context()
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
context.MR_saveOnlySelfWithCompletion(nil)
}
// I would normally call MR_saveOnlySelfWithCompletion here, but calling it inside the loop makes any UI block easier to spot
但是,我的假设是错误的。我looked into MR_saveOnlySelfWithCompletion 看到它依赖于
[self performBlock:saveBlock];
Asynchronously performs a given block on the receiver’s queue.
所以我有点困惑,因为我希望它不会因此而阻塞 UI。
然后我尝试了(我们称之为尝试#2)
let context = NSManagedObjectContext.MR_context()
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
context.MR_saveOnlySelfWithCompletion(nil)
}
}
这样就可以了,但感觉不对。
中发现了一些东西When sending messages to a context created with a queue association, you must use the performBlock: or performBlockAndWait: method if your code is not already executing on that queue (for the main queue type) or within the scope of a performBlock... invocation (for the private queue type). Within the blocks passed to those methods, you can use the methods of NSManagedObjectContext freely.
所以,我假设:
- Attempt#1 冻结 UI 因为我实际上是从主队列调用它而不是在 performBlock 的范围内
- 尝试 #2 有效,但我正在创建另一个线程,而上下文已经有自己的后台线程
所以of course我应该做的是使用saveWithBlock:
MagicalRecord.saveWithBlock { (localContext) -> Void in
for i in 1...1_000 {
User.MR_createInContext(context)
}
}
这将执行属于 PrivateQueueConcurrencyType 的操作 on a direct child of MR_rootSavingContext。 感谢 rootContextChanged,MR_rootSavingContext 之前的任何更改都将适用于 MR_defaultContext。
这么看来:
- MR_defaultContext 是显示数据的完美上下文
- 编辑最好在 MR_context(child of MR_defaultContext) 中完成
- long 运行 诸如服务器同步之类的任务最好使用 saveWithBlock 完成
它仍然没有得到的是如何使用 MR_save[…]WithCompletion()。我会在 MR_context 上使用它,但由于它在我的测试用例中阻塞了主线程,所以我看不到它何时变得相关(或者我错过了什么……)。
感谢您的宝贵时间:)
好的,我很少使用魔法记录,但既然你说你的问题比较笼统,我会尝试回答。
一些理论:创建上下文时,您会传递一个指示器,指示您是希望它绑定在主线程还是后台线程上
let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
"bound" 是指线程在内部由上下文引用。在上面的示例中,上下文创建并拥有一个新线程。此线程 不会自动使用,但必须显式调用 为:
context.performBlock({ () -> Void in
context.save(nil)
return
});
所以你的代码 'dispatch_async' 是错误的,因为上下文绑定到的线程只能从上下文本身引用(它是一个私有线程)。
从上面你要推断的是,如果上下文绑定到主线程,从主线程调用performBlock将不会 做任何与直接调用上下文方法不同的事情。
最后评论你的要点:
MR_defaultContext 是显示数据的完美上下文:NSManagedObject 必须从它所在的上下文访问 创建后,它实际上是 only 上下文,您可以为 UI 来自.
编辑最好在 MR_context(child of MR_defaultContext)中完成:编辑并不昂贵而且你应该跟着 上面的规则。如果您正在调用一个从主线程编辑 NSManagedObject 属性的函数(比如点击按钮) 您应该更新主要上下文。另一方面,保存是 昂贵,这就是为什么您的主要上下文不应链接到 直接持久存储,但只是将其编辑下推到根目录 具有持久存储的后台并发上下文。
long 运行 诸如服务器同步之类的任务最好使用 saveWithBlock 完成。
现在,尝试 1
for i in 1...1_000 {
let user = User.MR_createInContext(context) as User
}
context.MR_saveOnlySelfWithCompletion(nil)
无需为每个 object 创建都保存。即使 UI 没有被阻止也是浪费。
关于 MR_context。在魔法记录的文档中我看不到 'MR_context' 所以我想知道这是否是访问主要上下文的快速方法。如果是这样,它会阻塞。