Delete object in Core Data

后端 未结 2 1770
迷失自我
迷失自我 2020-12-29 21:31

How I can delete an object which I had added before with this code. Its a favorites section, in the begin, I add a gray star which adds an object coming from a fetch. Then I

2条回答
  •  感情败类
    2020-12-29 21:51

    Don't forget to save the Context after you have deleted a NSManagedObject. So here is the general code;

    NSManagedObjectContext * context = [self managedObjectContext];
    [context deleteObject:objectToDelete];
    
    NSError * error = nil;
    if (![context save:&error])
    {
        NSLog(@"Error ! %@", error);
    }
    

    In your case it should have the snippet after the for loop.

    for (NSManagedObject *managedObject in array) {
        [moc2 deleteObject:managedObject];
    }
    NSError * error = nil;
    if (![context save:&error])
    {
        NSLog(@"Error ! %@", error);
    }
    

提交回复
热议问题