我有一個簡單的應用程序,可以讓您在AddressBook中創建一組人員,因此羣組和人員處於一對多關係,因爲一個集團可以有多個人。自從我創建了我自己的Person模型後,這並不是多對多的。iPhone上的Coredata:只有當我重新啓動應用程序時,我纔可以刪除數據
添加數據沒有問題。
刪除數據不。如果我創建一個新人,我必須重新啓動應用程序才能刪除它或刪除該人所屬的組。否則,我會在控制檯中看到「EXC BAD ACCESS」。 NSZombieEnabled在環境中獲得-[CFString release]: message sent to deallocated instance 0x75140d0
。
我開始使用由XCode自動創建的CoreData東西,創建RootViewController(TableViewController的子類),我將它傳遞給上下文,並將其放入NavigationController中。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo il controller root e gli passo il context
RootViewController *rvc = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
rvc.context = [self managedObjectContext];
//Creo il navcon, gli associo il root e lo rendo visibile
navCon = [[UINavigationController alloc] initWithRootViewController:rvc];
[window addSubview:navCon.view];
[window makeKeyAndVisible];
[rvc release];
return YES;
}
的RootViewController的顯示組,然後點擊一排用於修改組的人,路過「nuovogruppo」
- (void)showPersoneControllerWithGruppo:(Gruppo *)nuovogruppo {
PersoneController *pc = [[PersoneController alloc] initWithStyle:UITableViewStylePlain];
pc.gruppo = nuovogruppo;
pc.context = self.context;
pc.delegate = self;
//NSLog(@"%@",[gruppi objectAtIndex:indexPath.row]);
[self.navigationController pushViewController:pc animated:YES];
[pc release];
(與該行相關的集團型)
}
這就是我如何刪除的人(gruppo
是集團模型中,這些人屬於,persone
是ARR AY充滿了對viewDidLoad中這些人,removeGPObject是的XCode生成的訪問方法(集團人的關係))
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[gruppo removeGPObject:[persone objectAtIndex:indexPath.row]];
[persone removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
NSError *error;
[context save:&error];
}
}
我希望有人能幫助我...
UPDATE
由於我對發送到已經發布的實例的消息有錯誤,我嘗試將所有[...發佈]行註釋掉,最後找出導致問題的原因。問題出在記錄的創建方法上,而不是在刪除方法中。這是我用來創建它的方法。 引起該問題的線路是[NomeCognome release]
如果有人能夠解釋爲什麼這條線路崩潰應用程序,我將不勝感激。
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *surname = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *NomeCognome = [NSString stringWithFormat:@"%@ %@", firstName, surname];
[firstName release];
[surname release];
Persona *persona = (Persona *)[NSEntityDescription insertNewObjectForEntityForName:@"Persona" inManagedObjectContext:context];
persona.ABRR = phone;
persona.NomeCognome = NomeCognome;
[phone release];
[NomeCognome release]; //This line makes the app crash!!! Why???
[gruppo addGPObject:persona];
NSError *error;
[context save:&error];
[self.delegate PersoneControllerDidSave:self];
[self loadContentAndReload:YES];
[self dismissModalViewControllerAnimated:YES];
}
爲什麼這行崩潰的應用程序?
確保你沒有在你的dealloc方法中釋放你的CoreData對象。如果不爲objc_exception_throw添加一個全局斷點,以檢查堆棧中的消息發送到正在發送的對象。 – Rog 2010-11-25 09:55:09