Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if NSManagedObject is saved?

Tags:

ios

core-data

How can I tell whether an NSManagedObject is persisted?

(As opposed to a new object that has not beed saved yet or was deleted.)

like image 327
Evgenii Avatar asked Sep 26 '14 04:09

Evgenii


People also ask

Will you ever pass Managedobject from one context to another context?

You cannot pass NSManagedObjects between multiple contexts, but you can pass NSManagedObjectIDs and use them to query the appropriate context for the object represented by that ID.

How do I save in Core Data?

Right-click on your project's folder in the project navigator and then New File… In the new window, type “data” in the top right corner, select Data Model, and press Next. Give it a name, and save it. Now, let's add all the necessary code to connect Core Data with our project.

What is context in Core Data?

From your perspective, the context is the central object in the Core Data stack. It's the object you use to create and fetch managed objects, and to manage undo and redo operations. Within a given context, there is at most one managed object to represent any given record in a persistent store.

What is NSManagedObject in Core Data?

In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.


1 Answers

check if its object id is persistent

BOOL isSaved = !object.objectID.isTemporaryID;

documentation about isTemporaryID:

"YES if the receiver is temporary, otherwise NO. Most object IDs return NO. New objects inserted into a managed object context are assigned a temporary ID which is replaced with a permanent one once the object gets saved to a persistent store."

like image 76
Daij-Djan Avatar answered Oct 13 '22 01:10

Daij-Djan