user3781236
user3781236

Reputation: 728

How to detect changes in core data relationship?

In core data, we're trying to determine the deltas in our one-to-one and one-to-many relationships since the last save event (either removed, added, or updated objects in the relationship). We've tried to use the NSMangagedObjectContext method updatedObjects, but it only ever returns information on the attributes changed, and no information on the relationships changed. We've searched the Apple documentation and other sources and considered writing custom Managed Object accessor methods, but saw that it was highly discouraged.

What is the best way to figure out the deltas in an NSManagedObject relationship?

Upvotes: 2

Views: 773

Answers (1)

Mundi
Mundi

Reputation: 80265

updatedObjects should return all objects in the object graph that have changes. You just need to make sure that:

  1. you are using the same managed object context, and
  2. the changes have not been saved yet.
  3. you have added the objects you want to observe to the context's registeredObjects

You can accomplish the last point e.g. by fetching all instances of the entity you are interested in.

Please note that you can also use the NSManagedObjectContextObjectsDidChangeNotification to react to changes in the object graph.

Upvotes: 2

Related Questions