【发布时间】:2010-09-12 09:38:56
【问题描述】:
偶尔的读者和第一次提问者,所以请温柔:)
我正在创建一个托管对象(帐户),它被传递到一个子视图控制器中,在该控制器中它被设置在一个保留的属性中。
Account * account = [[Account alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
AddAccountViewController *childController = [[AddAccountViewController alloc] init];
childController.title = @"Account Details";
childController.anAccount = account;
childController.delegate = self;
[self.navigationController pushViewController:childController animated:YES];
[childController release];
[account release];
视图控制器接口:
@interface AddAccountViewController : UIViewController {
}
@property (nonatomic, retain) IBOutlet UITextField * usernameTextField;
@property (nonatomic, retain) IBOutlet UITextField * passwordTextField;
@property (nonatomic, retain) Account * anAccount;
@property (nonatomic, assign) id <AddAccountDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)add:(id)sender;
- (IBAction)textFieldDone:(id)sender;
@end
所以在代码示例 1 中,我已经发布了帐户对象,因为我不再对该方法感兴趣。因为它被 AddAccountViewController 保留,所以我在 AddAccountViewController 的 dealloc 中有一个条目来释放它。
但是,当我从 ManagedObjectContext 中删除对象时,应用程序崩溃并出现以下(相当不清楚)错误:
Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
_Unwind_Resume called from function _PFFaultHandlerLookupRow in image CoreData.
经过多次调试和拔毛后,我发现如果我不在 AddAccountViewController 的 dealloc 方法中释放帐户,则应用程序会持续正常运行并且似乎不会根据仪器泄漏。
任何人都可以阐明发生了什么吗?我从有关属性的文档中了解到,需要释放那些保留的属性。我错过了什么?
更新以回答 Kevin 的问题
从 ManagedObjectContext 中删除对象的代码位于 RootViewController(即持有子控制器)中
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context 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();
}
}
}
【问题讨论】:
-
你能显示从 NSManagedObjectContext 中删除它的代码吗?这会发生在 AddAccountViewController 内部还是其他地方?
-
childController.anAccount = account;此行不保留account。它将它复制到anAccount。这根本不会 +1 它的保留,您只需在AddAccountViewcontroller的 dealloc 方法中释放anAccount。 -
@Thomas:
@property (nonatomic, retain) Account * anAccount;为什么不保留? -
托马斯,它不保留吗?它是声明为
retain而不是copy的属性的合成集。 -
账户对象创建后是否保存对象上下文?
标签: iphone objective-c cocoa-touch core-data ios