Below Error is thrown while saving after deletion.
An NSManagedObjectContext cannot delete objects in other contexts.
I also checked if the managedobjectcontext from which the data is being fetched is same as the managedobjectcontext which is deleting the data. They both turns out to be equal. You can see below comparison .
NSManagedObjectContext *managedobjectcontext=[Singleton managedObjectContext];
NSArray *allprebuyers=[Fetchsavefromcoredata arrayfromentityresult:@"Buyer"];
for(int i=0;i<[allprebuyers count];i++)
{
Buyer *buyerobj=[allprebuyers objectAtIndex:i];
NSLog(@"class name : %@",NSStringFromClass([buyerobj class]));
//object comparison for fetched moc and moc which is deleting, log says Equal.
if ([[buyerobj managedObjectContext] isEqual:managedobjectcontext])
{
NSLog(@"Equal");
}
else
{
NSLog(@"Not Equal");
}
[managedobjectcontext deleteObject:buyerobj];
NSError *error=nil;
[managedobjectcontext save:&error];
}
I have been trying to resolve this issue, any help will be appreciated.
You have to options to delete a managed object.
1) In the owning context
2) In another context
You cannot delete an object in a context if the object is registered with another context.
Also, please note a context is associated either with a thread or with a queue, main or private. Be sure you access the context from the right thread/queue.