在 [managedObjectContext deleteObject:device] 之后;仍在设备对象中显示数据。无法从数据库中删除数据。如何删除数据
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Reserve" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// NSSortDescriptor tells defines how to sort the fetched results
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSLog(@"sort count%d",[sortDescriptors count]);
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(number = %d)", [sender tag]];
// [fetchRequest setPredicate:predicate];
[fetchedResultsController.fetchRequest setPredicate:predicate];
self.fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"Count of array::::%d",[fetchedObjects count]);
NSManagedObject *device = [fetchedObjects lastObject];
NSLog(@"device::::%@",device);
[managedObjectContext deleteObject:device];
NSLog(@"device::::%@",[fetchedObjects lastObject]);
提前致谢