0

在保存對象時,我遇到了使用MagicalRecord時出現的問題。在保存對象時執行使用MagicalRecord時出現錯誤訪問

節約使用上下文:

- (void)saveContext { 
    [[NSManagedObjectContext defaultContext] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { 
     if (success) { 
      NSLog(@"You successfully saved your context."); 
     } else if (error) { 
      NSLog(@"Error saving context: %@", error.description); 
     } 
    }]; 
} 

這個原因導致我的應用程序隨機崩潰,並EXEC_BAD_ACCESS上[[self MR_defaultContext] mergeChangesFromContextDidSaveNotification:notification];

我用另一種方法:

//get correct order based on indexPath 
Order *orderToComplete = [self objectInOrdersAtIndex:indexPath.section]; 
//set order as completed 
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) 
{ 
    Order *localOrder = [orderToComplete inContext:localContext]; 
    [localOrder setIsCompletedValue:YES]; 
}completion:^(BOOL success, NSError *error) 
{ 
    if(success) 
     NSLog(@"You successfully saved your context."); 
    else 
     NSLog(@"Error saving context: %@", error.description); 
}]; 

但還是隨機崩潰。

我的應用程序不是多線程的應用程序。 這裏是一個鏡頭: enter image description here

有沒有人有任何想法?

+0

具有相同的問題。你解決了嗎? – 2014-11-22 18:55:56

回答

0

我可以解決我的問題。但找不到確切的原因。

步驟,您應該遵循

  1. 清除所有的目標方法的代碼,只是創建一個對象,並保存呢?確實保存?如果是這樣的話:
  2. 添加你的代碼並一步一步地測試你可以在你的代碼中找到它,managedObjectStore將進入不一致狀態。對我來說,這是因爲調用委託方法。委託方法調用後,我放了[NSManagedObjectContext saveContext]。它固定

即時通訊使用2.2版 我可以找到問題發生的地方,但我不明白爲什麼! 但我在這裏: 一個新項目將被推送到設備從網絡,所以如果它是第一個記錄單元格應自動被選中。然後我調用detailsView控制器委託setItems來更新它的項目。之後,KVO將通知有關更改並嘗試更新tableView: 想象沒有記錄,一個新項目將被推送到設備,其子項目將通過代理方法添加到detailsViewController

self.subItems =子項目;

landscape

步驟: 1-更新視圖 2-保存上下文

委託方法在每一個推調用兩次。 然後KVO通知有一個子項目的變化NSKeyValueChangeSetting 第二次推後如果第二次推包含新的子項目KVO第一次通知有一個插入項目subItemsNSKeyValueChangeInsertion,然後如我所說的委託方法調用兩次,KVO通知有一個變化NSKeyValueChangeSetting。在重新加載桌面應用程序崩潰後的這一步!

NSNumber *kind = [change objectForKey:NSKeyValueChangeKindKey]; 
     //if we are setting new items to the items array; i.e. when we select a new row 
     if ([kind integerValue] == NSKeyValueChangeSetting) { 
      // adding the all the new items at top of the tableView 
      NSArray *newItems = [change valueForKey:@"new"]; 
      [self.tableView reloadData]; 

     } 
     //if we are adding one new order to the orders array 
     else if ([kind integerValue] == NSKeyValueChangeInsertion) 
     { 
      // adding the new order at top of the tableView 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop]; 
     } 

最後我嘗試了一次調用這個委託來解決這個崩潰問題。但我無法理解爲什麼上述錯誤發生!

https://github.com/magicalpanda/MagicalRecord/issues/877