I am trying to delete a Core Data Entity and no matter what delete rule I use, it just won’t work. It does not do anything at all.
I am using Firefox’s Sqlite Add-on to browse the core data database and I can see all the rows there after the delete code runs.
My code for deletion looks like this
+(void)deleteCustomerWithID:(int)customerid inManagedObjectContext:(NSManagedObjectContext *)context
{
Customer *customer = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Customer"];
request.predicate = [NSPredicate predicateWithFormat:@"id = %d", customerid];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"organization" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *customers = [context executeFetchRequest:request error:&error];
if (!customers || ([customers count] > 1)) {
// handle error
} else if (![customers count]) {
// customer not found
} else {
customer = [customers lastObject];
[context deleteObject:customer];
NSError *error;
if(![context save:&error]){
NSLog(@"Unresolved error series %@, %@", error, [error userInfo]);
}
}
}
Any ideas what am I doing wrong?
I am not sure what went wrong but I ended up deleting all the relationships in Entities and then re-creating them. That seems to have solved the issue. I can now successfully Cascade Delete.