在熟悉核心數據時,我發現自己在嘗試添加數據時要通過各種視圖控制器(VC)的問題感到困惑。添加核心數據對象從一個segue
例如,在CoreDataRecipes項目,蘋果提供作爲一個例子(http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html)它們用下面的辦法
當用戶想要向配方添加到在主表視圖呈現食譜的列表,並點擊Add按鈕,主表視圖控制器(稱爲RecipeListTableViewController)創建如下新的管理對象(配方):被傳遞到RecipeAddViewController
- (void)add:(id)sender {
// To add a new recipe, create a RecipeAddViewController. Present it as a modal view so that the user's focus is on the task of adding the recipe; wrap the controller in a navigation controller to provide a navigation bar for the Done and Save buttons (added by the RecipeAddViewController in its viewDidLoad method).
RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:@"RecipeAddView" bundle:nil];
addController.delegate = self;
Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];
addController.recipe = newRecipe;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addController release];
}
這個新創建的對象(配方)。該RecipeAddViewController有兩種方法,保存和取消,具體如下:
- (void)save {
recipe.name = nameTextField.text;
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:recipe];
}
- (void)cancel {
[recipe.managedObjectContext deleteObject:recipe];
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:nil];
}
我疑惑這個設計方法。爲什麼RecipeListViewController會在我們知道用戶是否想要輸入新配方名稱並保存新對象之前創建對象?爲什麼不將managedObjectContext傳遞給addRecipeController,並等待用戶點擊保存以創建對象並使用數據填充其字段?這避免瞭如果沒有新的配方添加,必須刪除新的對象。或者爲什麼不在RecipeListViewController和RecipeAddController之間來回傳遞配方名稱(字符串)?
我這麼問是因爲我在努力瞭解何時塞格斯之間傳遞一個字符串,什麼時候該傳球的對象,而當通過managedObjectContexts ...
大加讚賞任何指導,含。任何有關討論設計理念的鏈接。
感謝您的回覆。這看起來非常優雅,並且避免了我擔心的事情,即addRecipe進程的中斷可能最終會在我的managedObjectContext中留下空的記錄。關於這種方法我還有幾個問題。首先,我已經閱讀了併發類型,並且很好奇你爲什麼選擇NSMainQueueConcurrencyType作爲編輯上下文。第二,爲什麼是NSParameterAssert(recipeForEditing);需要? –
更新:當我嘗試使用Matthias B.的方法時,我在運行時得到以下錯誤:***終止應用程序,由於未捕獲異常'NSInvalidArgumentException',原因:'父NSManagedObjectContext必須使用NSPrivateQueueConcurrencyType或NSMainQueueConcurrencyType。' ...這引出了一個問題,當我在應用程序委託中聲明原始的NSManagedObjectContext時,我需要使它成爲這些類型中的一種,如果是,哪一個更好? –