Advaith
Advaith

Reputation: 1117

NSManagedObject delete not working properly

I am deleting a NSManagedObject using the statement

[managedObjectContext deleteObject:obj];

And after that am storing the changes to that persistent store. And when I tried to view my actual table using some database viewer, I could see the object still there in the table. I was confused and I made a refresh call after deletion as below (just before saving into persistent store).

[managedObjectContext refreshObject:obj mergeChanges:YES];

And when I tried to view the table now, I couldn't see the object. It's working now, but I am confused because of 2 reasons.

1: I am deleting many objects in many other places, and am not making a refresh every where, though am saving into the persistent store.

2: I don't understand the concept of refreshing an object that was already deleted.

Can any one help me out? Thanks in advance!

Upvotes: 0

Views: 208

Answers (1)

Alex
Alex

Reputation: 1581

it is not easy to answer without a little bit more context.

That said, if you delete a managed object, fetchedResultsController won't be updated unless they were created on the same managedobjectContext. In other words two database fetch if issued from two different managed context will have an out of sync view of the database state.

I would advise you to check if this isn't the case.

Also, you shouldn't have to perform a refresh - except in very specific cases - NSFetchresultController is notified about the database changes, provided you implemented the NSFetchedResultsControllerDelegate protocol.

Upvotes: 2

Related Questions