2015-06-17 45 views
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。

回答

6

您的對象在保存塊中沒有發生變化。我在這裏看到兩個問題。

  1. 當你需要在localContext這是您的保存塊保存上下文中創造他們你正在創建的MR_defaultContext你的新對象。

試試這個:

- (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion { 
    NSMutableArray *mutableArray = [NSMutableArray array]; 


    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { 
     for (NSDictionary *dictionary in quizzess) { 
      Quiz *quiz = [Quiz MR_createInContext:localContext]; 
      [quiz fromDictionary:dictionary]; 
      [mutableArray addObject:quiz]; 
     } 
    } completion:^(BOOL contextDidSave, NSError *error) { 
     BlockSafeRun(completion, mutableArray, contextDidSave, error); 
    }]; 
} 
  • 你的對象是你開始導入新對象之前,實際上沒有被刪除。這可能不會導致你的上下文沒有改變,但我也會討論這個。
  • [Quiz MR_truncateAll]只是將所有的Quiz對象的deleted屬性true。這意味着下次保存或處理上下文時,更改將被保存。

    因此,當您創建新的保存上下文時,上下文仍然具有這些對象。我不確定你的fromDictionary方法在做什麼,但是如果它依賴於一個數據庫,那麼它不會擁有它。

    你需要做的是這樣的:

    - (void)updateStorageWithQuizzess:(NSArray *)quizzess completion:(void(^)(NSArray *quizzess, BOOL succes, NSError *error))completion { 
        NSMutableArray *mutableArray = [NSMutableArray array]; 
    
    
        [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { 
         [Quiz MR_truncateAllInContext:localContext]; 
         [localContext processPendingChanges]; 
    
         for (NSDictionary *dictionary in quizzess) { 
          Quiz *quiz = [Quiz MR_createInContext:localContext]; 
          [quiz fromDictionary:dictionary]; 
          [mutableArray addObject:quiz]; 
         } 
        } completion:^(BOOL contextDidSave, NSError *error) { 
         BlockSafeRun(completion, mutableArray, contextDidSave, error); 
        }]; 
    } 
    

    這樣一來,你在儲蓄方面刪除的對象。您還必須記得在保存上下文時調用processPendingChanges以從上下文中刪除對象,而不是將其標記爲要刪除。

    +0

    謝謝你的回答。但是這些更改不適用於我,contextDidSave仍然== 0。 fromDictionary只是從NSDictionary中填充一些參數。 – BergP

    +0

    即使我根本刪除了這些行,上下文也不會保存更改:[Quiz MR_truncateAllInContext:localContext]; [localContext processPendingChanges]; – BergP

    +0

    哎呀!我第一次錯過了一個關鍵問題!我已經更新了我的答案。確保在保存上下文中創建新的實體! ;) – AnthonyM