managedObjectContext deleteObject:匹配不从Core数据库中删除数据?

时间:2012-08-09 09:42:27

标签: iphone ios core-data nsmanagedobjectcontext

我试图从ios应用程序中的核心数据库中删除行,但它无法正常工作,

if (editingStyle == UITableViewCellEditingStyleDelete) {

     appDelegate = [[UIApplication sharedApplication] delegate];   
        NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"PRODUCTTABLE" inManagedObjectContext:appDelegate.managedObjectContext];        
        NSFetchRequest *request = [[NSFetchRequest alloc] init];        
        [request setEntity:entityDesc];        
        NSError *error; 
        NSManagedObject *matches = nil;
        NSArray *objects = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
        NSLog(@"Array Count....%d",[objects count]);
        matches=[objects objectAtIndex:([indexPath row])];
        NSLog(@"...%@",[matches valueForKey:@"productID"]);
        NSLog(@"...%@",[matches valueForKey:@"pName"]);
        NSLog(@"...%@",[matches valueForKey:@"pPrice"]);
        NSLog(@"...%@",[matches valueForKey:@"pDate"]);           
        [appDelegate.managedObjectContext deleteObject:matches];


    }   

以下代码的o / p:

Array Count....12
...ABC1226
...ABC-005
...599.39
...2012-08-09 05:07:50 +0000

使用相同的NSManagedObject从核心数据中获取数据。为什么删除不更新核心数据?

1 个答案:

答案 0 :(得分:10)

更改后保存上下文。

NSError * error = nil;
[appDelegate.managedObjectContext deleteObject:matches];
[appDelegate.managedObjectContext save:&error];