Home Distinguishing between objects fetched from the in-memory Managed Object Context and the Persistent Store
Reply: 1

Distinguishing between objects fetched from the in-memory Managed Object Context and the Persistent Store

Apophenia Overload
1#
Apophenia Overload Published in 2017-11-14 20:10:43Z

In an iOS app, I am creating NSManagedObject subclass objects using a framework that parses JSON objects and then inserts them into an NSManagedObjectContext. However, I want to distinguish between those newly created objects and ones that have already been saved into the persistent store. That's because I may need to update or overwrite some of the objects in the store. When I execute an NSFetchRequest, it simply returns all of the registered objects, both objects fetched from the store, and the ones I've created locally, in-memory. How would I be able to distinguish between the two types of objects?

Tom Harrington
2#
Tom Harrington Reply to 2017-11-14 21:05:23Z

You can tell if a managed object has been saved by looking at its object ID. Check myObject.objectID.isTemporaryID, which will be true until you first save the object and false from then on.

However as @Paulw11 alludes to in a comment, in this case it's generally better to find out if you already have a local object before creating a new one. That avoids the issue of having duplicates. You can either update values on the existing object or delete it and replace it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328684 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO