3
我想堅持新的實體:神奇的記錄,不能保存對象:contextDidSave == NO,誤差=零
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[[NSManagedObjectContext MR_context] MR_saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
或者這樣:
- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion {
NSMutableArray *mutableArray = [NSMutableArray array];
[Quiz MR_truncateAll];
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (NSDictionary *dictionary in quizzess) {
Quiz *quiz = [Quiz MR_createEntity];
[quiz fromDictionary:dictionary];
[mutableArray addObject:quiz];
}
} completion:^(BOOL contextDidSave, NSError *error) {
BlockSafeRun(completion, mutableArray, contextDidSave, error);
}];
}
但在完成塊我收到contextDidSave == NO,錯誤== nil。 所以我無法弄清楚出了什麼問題。 我做了一些明顯的錯誤嗎?我該如何調試該問題?從MR
//////
2015-06-17 20:39:27.061 HITO[6733:618953] Set root saving context: <NSManagedObjectContext: 0x16dbe070>
2015-06-17 20:39:27.062 HITO[6733:618953] Created new main queue context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.063 HITO[6733:618953] Set default context: <NSManagedObjectContext: 0x16e855b0>
2015-06-17 20:39:27.316 HITO[6733:618953] [HockeySDK] WARNING: Detecting crashes is NOT enabled due to running the app with a debugger attached.
2015-06-17 20:39:28.829 HITO[6733:618953] Created new private queue context: <NSManagedObjectContext: 0x16d57870>
2015-06-17 20:39:28.831 HITO[6733:619027] Created new private queue context: <NSManagedObjectContext: 0x16ea4ec0>
2015-06-17 20:39:28.841 HITO[6733:619027] NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING
更新
以下代碼:
- (void) MR_saveWithOptions:(MRSaveOptions)saveOptions completion:(MRSaveCompletionHandler)completion;
{
__block BOOL hasChanges = NO;
if ([self concurrencyType] == NSConfinementConcurrencyType)
{
hasChanges = [self hasChanges];
}
else
{
[self performBlockAndWait:^{
hasChanges = [self hasChanges];
}];
}
if (!hasChanges)
{
MRLogVerbose(@"NO CHANGES IN ** %@ ** CONTEXT - NOT SAVING", [self MR_workingName]);
if (completion)
{
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, nil);
});
}
return;
}
所以,hasChanges返回NO。
謝謝你的回答。但是這些更改不適用於我,contextDidSave仍然== 0。 fromDictionary只是從NSDictionary中填充一些參數。 – BergP
即使我根本刪除了這些行,上下文也不會保存更改:[Quiz MR_truncateAllInContext:localContext]; [localContext processPendingChanges]; – BergP
哎呀!我第一次錯過了一個關鍵問題!我已經更新了我的答案。確保在保存上下文中創建新的實體! ;) – AnthonyM