Anton Unt
Anton Unt

Reputation: 1875

What is the difference between self.managedObjectContext and managedObjectInstance.managedObjectContext?

So, I have the CoreData entities Book, Bookmark, Highlight. A book contains information about the book and an NSSet of Bookmarks.

I want to delete the bookmarks present in one book and then immediately repopulate them with my set.

This is a code that I have got from another project and I can not seem to get why they have used different kinds of deleteObject method.

They go like:

for (Bookmark *bookmark in book.bookmarks) {
    [bookmark.mamagedObjectContext deleteObject:bookmark];
}

What's the difference between using self.managedObjectContext and bookmark.managedObjectContext. Also, either doesn't seem to have any effect on the contents and I get an error while trying to delete all the bookmarks in the forin loop and then adding a new set by a forin loop again.

Please mention in comments if more information is required.

Upvotes: 0

Views: 152

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

The managedObjectInstance.managedObjectContext returns the managed object context with which the receiver is registered.

In other words, if you created, for example, a bookmark in the main thread context (the one associated with the context created in the main thread), bookmark.managedObjectContext will point to that.

Are you using different threads? Different contexts? What type of error do you have? Share it adding an edit to your question.

Upvotes: 2

Related Questions