我在删除条目时遇到了一些困难Core Data
。
它可以工作,但 NSManagedObject 数据没有值(错误),因此有时会删除错误的条目。
在我的deleteWholeDatabase方法中,对象填充了值。并且在deleteEntryFromDatabase方法中没有值。但为什么?
这是我的方法:
-(NSArray*)databaseRequest{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:currentEntity inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
return items;
}
-(void)deleteWholeDatabase{
for (NSManagedObject *objectToDelete in [self databaseRequest]) {
[managedObjectContext deleteObject:objectToDelete];
NSLog(@"%@ object deleted",objectToDelete);
}
[self saveContext];
NSLog(@"All entries deleted!");
}
-(void)deleteEntryFromDatabase:(NSManagedObjectContext*)context forEntry:(NSIndexPath*)indexPath{
NSManagedObject *objectToDelete=[[self databaseRequest] objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:[managedObjectContext objectWithID:[objectToDelete objectID]]];
[self saveContext];
NSLog(@"%@ deleted!",[managedObjectContext objectWithID:[objectToDelete objectID]]);
}