Coredata在iphone:我可以删除数据,只有我重新启动应用程序 [英] Coredata on iphone: I can delete data only if I restart the application

查看:93
本文介绍了Coredata在iphone:我可以删除数据,只有我重新启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,可以让您在地址簿中创建人员组。因此,组和人员是一对多的关系,因为一个组可以有多个人。这不是一个多对多,因为我创建了我自己的人的模型。



添加数据工作没有问题。



删除资料并非如此。如果我创建一个新的人,我必须重新启动应用程序删除它或删除该人所属的组。否则我在控制台中得到一个EXC BAD ACCESS。使用NSZombieEnabled在环境中我得到 - [CFString release]:消息发送到解除分配的实例0x75140d0



与XCode自动创建的CoreData东西,创建RootViewController(TableViewController的子类),我传递它的上下文并将其放在NavigationController。

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//创建控制器根目录gli passo il上下文
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 是一个由viewDidLoad ,removeGPObject是由XCode(Group to Persons关系)生成的访问器方法)

   - (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属性:(ABPropertyID) (ABMultiValueIdentifier)标识符{

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:@PersonainManagedObjectContext:context];
persona.ABRR = phone;
persona.NomeCognome = NomeCognome;
[phone release];
[NomeCognome release]; //这行让应用崩溃!为什么???

[gruppo addGPObject:persona];

NSError * error;
[context save:& error];
[self.delegate PersoneControllerDidSave:self];
[self loadContentAndReload:YES];
[self dismissModalViewControllerAnimated:YES];
}

为什么该行会导致应用程式崩溃?








   - (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];
[tableView reloadData];
}

它现在可以工作了。


I've a simple applications that lets you create groups of people form persons in your AddressBook... So Groups and Persons are in a one-to-many relationships, since a Group can have multiple persons. That's not a many-to-many since I create my own model of Person.

Adding data works without problems.

Deleting data doesn't. If I create a new Person, I must restart the app to delete it or the to delete the Group that Person belongs. Otherwise I get a "EXC BAD ACCESS" in the console. With NSZombieEnabled in the enviroment I get -[CFString release]: message sent to deallocated instance 0x75140d0.

I start with the CoreData stuff automatically created by XCode, create the RootViewController (subclass of TableViewController), I pass it the context and put it in a 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;

}

The RootViewController shows the Groups, then clicking on a row lets you modify persons in that group, passing the "nuovogruppo" (the Group Model associated with that row)

- (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];

}

And this is how I delete the person (gruppo is the Group model these persons belong to, persone is an array filled with these persons on viewDidLoad, removeGPObject is an accessor method generated by XCode (Group to Persons relationship))

- (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];
}

}

I hope someone can help me...

UPDATE

Since I was having errors about messages sent to already released instances I tried commenting out all the [... release] lines and finally find out what was causing the problem. The problem was in the creation method of the record and not in the deleting method. Here is the method I use to create it. The line that was causing the roblem is [NomeCognome release] I'd be very grateful if someone could explain me why this line crashes the app.

- (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];
}

Why that line crashes the app?

解决方案

Use reloadData method at the end as the following.

- (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];
    [tableView reloadData];
}

It may work now.

这篇关于Coredata在iphone:我可以删除数据,只有我重新启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆