我有一个由UIViewControllers在app委托中调用的方法,它删除与所选对象名称匹配的核心对象。它从UIListView中删除是正确的,刷新方法(检查所有对象的核心数据)不会将其带回列表视图。但是,当我关闭应用程序并重新启动它时,它会带回刚刚删除的对象。这是方法:
-(void)deleteObject:(NSString *)configName
{
//Generates the request context for core data
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"StoredSetups" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(config_name = %@)", configName];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects.count == 0)
{
NSLog(@"No Matches");
}
else
{
matches = objects[0];
[context deleteObject:matches];
}
}
非常感谢任何帮助或反馈。提前谢谢!
答案 0 :(得分:1)
你必须在删除后保存上下文,如下所示:
if (objects.count == 0)
{
NSLog(@"No Matches");
}
else
{
matches = objects[0];
[context deleteObject:matches];
[context save:&error];
}