tjg184
tjg184

Reputation: 4676

How does one effectively handle temporary objects in Core Data since the objectID changes between temporary objects and permanent objects?

What is the best way to handle temporary objects in Core Data? I've seen solutions where temporary contexts are created, where they are inserted into nil contexts, etc.

However, here's the issue I'm seeing in both of these solutions. I'm using Core Data for my object model and and in some of my views store a NSSet of Core Data objects. The problem I have is when the object is stored, the objectID changes which effectively invalidates anything stored in any NSSet since the isEqual and hash are now different. While I could invalidate the object stored in the NSSet, it often is not practical and certainly not always easy.

Here's the things I've considered:

1) override isEqual method and hash on NSManagedObject (obviously bad)

2) do not place any NSManagedObject in a NSSet (use a NSDictionary where the key is always fixed)

3) use an entirely different type to store in NSSet where I could correctly implement the isEqual and hash code methods

Does anyone have a better solution for this?

Upvotes: 4

Views: 1247

Answers (4)

Simon Lee
Simon Lee

Reputation: 22334

You could possibly subclass NSManagedObject and override the willSave and didSave methods to remove and then re-add you objects to your set.

I actually ended up using a different approach, that of using a NIL context and providing a base class to handle insertion into a context. It works really well and is the cleanest solution I have found. Code can be found here... Temporary Core Data

Upvotes: 0

adonoho
adonoho

Reputation: 4339

However, here's the issue I'm seeing in both of these solutions. I'm using Core Data for my object model and and in some of my views store a NSSet of Core Data objects. The problem I have is when the object is stored, the objectID changes which effectively invalidates anything stored in any NSSet since the isEqual and hash are now different.

tjg184,

Your problem here is not the transition to permanent IDs but that your container class depends upon an immutable hash. Hence, change your container class to an array or dictionary and this problem goes away. (You give up uniquing with an array but that is easy to handle with a trip through a transient set to perform the uniquing.)

Andrew

Upvotes: 1

Elise van Looij
Elise van Looij

Reputation: 4232

ManagedObjects in an NSSet -- that sounds like a Core Data relationship. Why not simply store your temporary managedObjects in a relationship, and have Core Data take care of the problems you're now running into. Then you can concentrate on when and how to delete the temporary objects, or break the relationship or whatever is needed.

Upvotes: 3

Alfonso
Alfonso

Reputation: 8482

A possible solution would be to convert the temporary IDs to permanent ones using [NSManagedObjectContext obtainPermanentIDsForObjects:error:].

But be aware that this may be expensive, especially if you have a lot of objects you need to process this way.

Upvotes: 0

Related Questions