关于 MagicalRecord,MR_saveNestedContexts 和 MR_save 方法有什么区别 [英] Regarding MagicalRecord, what is the difference between the methods MR_saveNestedContexts and MR_save
问题描述
在 MagicalRecord github 文档中,它指出:
In the MagicalRecord github docs it states:
MagicalRecord 提供了一个后台保存队列,以便保存所有数据在主线程外在后台执行.这表示可能需要使用 MR_saveNestedContexts 而不是典型的 MR_save 方法,以便将您的更改一直保存到您的持久存储.
MagicalRecord provides a background saving queue so that saving all data is performed off the main thread, in the background. This means that it may be necessary to use MR_saveNestedContexts rather than the typical MR_save method in order to persist your changes all the way to your persistent store.
看源码,我想不出这两种方法减去dispatch_async
命令的区别.我看到他们都将所有嵌套的上下文保存到根目录,因此持久保存到商店.但是为什么以及在什么情况下我会使用一种而不是另一种?
looking at the source, I can't figure out the difference between these two methods minus the dispatch_async
command. I see that they both save all nested contexts up to the root, therefore persisting to the store. But why and in what situation would I use one over the other?
此外,只要将嵌套上下文保存到一级(不坚持存储),我假设我仍然会使用 NSManagedObjectContext 的 - (BOOL)save:(NSError **)error
方法?
Also, as far as just saving a nested context up one level (without persisting to store) I'm assuming I would still use NSManagedObjectContext's - (BOOL)save:(NSError **)error
method?
以下是两种方法的源码.
Below is the source code of the two methods.
- (void) MR_save {
[self MR_saveErrorHandler:nil];
}
- (void) MR_saveErrorHandler:(void (^)(NSError *))errorCallback {
[self performBlockAndWait:^{
[self MR_saveWithErrorCallback:errorCallback];
if (self.parentContext) {
[[self parentContext] performBlockAndWait:^{
[[self parentContext] MR_saveErrorHandler:errorCallback];
}];
}
}];
}
- (void) MR_saveNestedContexts {
[self MR_saveNestedContextsErrorHandler:nil];
}
- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback {
[self MR_saveNestedContextsErrorHandler:nil completion:nil];
}
- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback completion:(void (^)(void))completion {
[self performBlock:^{
[self MR_saveWithErrorCallback:errorCallback];
if (self.parentContext) {
[[self parentContext] performBlock:^{
[[self parentContext] MR_saveNestedContextsErrorHandler:errorCallback completion:completion];
}];
} else {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
}
}
}];
}
推荐答案
'MR_save' 和 'MR_saveNestedContexts' 之间的相似性在 2.0.8 版本中发生了变化(我使用的是 2.0.7).
The similarity between 'MR_save' and 'MR_saveNestedContexts' was changed in version 2.0.8 (I was using 2.0.7).
这是 github 上的提交:https://github.com/magicalpanda/MagicalRecord/commit/f7c4350e9daf7d90eec83ba5eafeccfa7af34312
Here is the commit on github: https://github.com/magicalpanda/MagicalRecord/commit/f7c4350e9daf7d90eec83ba5eafeccfa7af34312
以及讨论:https://github.com/magicalpanda/MagicalRecord/issues/305
总而言之,在 2.0.8 版本中,'MR_save' 现在只保存当前上下文,'MR_saveNestedContexts' 递归地保存到最顶层的上下文.
So to summarize, in version 2.0.8, 'MR_save' now only saves the current context, and 'MR_saveNestedContexts' recursively saves up to the topmost context.
这篇关于关于 MagicalRecord,MR_saveNestedContexts 和 MR_save 方法有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!