2014-02-22 49 views
1

我試圖創建一個更新函數,也允許用戶取消該過程。 我正在使用父子managedObjectContext,並將父比賽併發性設置爲NSMainQueueConcurrencyType。我的計劃是不要調用父上下文的保存來取消更新。當我測試了我的理論並將所述保存行註釋掉後,我發現managedObject仍然更新。我究竟做錯了什麼?保存在子上下文中自動保存到父上下文

部分更新功能代碼:

NSManagedObjectContext *bgContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; 
[bgContext setParentContext:self.managedObjectContext]; 
[bgContext setUndoManager:nil]; 

[bgContext performBlockAndWait:^{ 

    for (NSDictionary *itemDictionary in items) { 
     //update cancelled 
     if (status == -1) { 
      return; 
     } 

     //Function to get a single managedObject by querying the main context. 
     NSArray *array = [self queryEntity:entityName withResId:[dictionary objectForKey:@"res_id"]]; 

     //get the object id of queried managedobject; context -> mainContext 
     ENTITY *object = (ENTITY *)[context objectWithID:[[array firstObject] objectID]]; 
     object.data = @"something"; //change data 

     count++; 

     //save every 100 
     if(count%100 == 0) { 
      [bgContext performBlockAndWait:^{ 
       NSError *error; 
       if(![bgContext save:&error]) { 
        NSLog(@"insert error child: %@", [error localizedDescription]); 
       } 
      }]; 
     } 
    } 

    //save excess 
    if(count%100 != 0) { 
     [bgContext performBlockAndWait:^{ 
      NSError *error; 
      if(![bgContext save:&error]) { 
       NSLog(@"insert error child: %@", [error localizedDescription]); 
      } 
     }]; 
    } 

    //comment out to prevent saving to parent 
    /*[context performBlockAndWait:^{ 
     NSError *error; 
     if(![context save:&error]) { 
      NSLog(@"insert error parent: %@", [error localizedDescription]); 
     } 
    }];*/ 
}]; 

回答

相關問題