2

I am working with core data. I want to update a managed object on few events of my application. That managed object is either created or fetched (if exist) on app launch. I know that managed object is not thread safe so I am not storing the managed object in memory, instead of it I am storing objectID as it is thread safe. I am storing the objectID once the object is created and saved to context so I am sure that stored objectID is permanent and not temporary.

On the events where I want to update my object I have following code:

self.managedObjectContext.performBlock {
    let myObject = self.managedObjectContext.objectWithID(objectId) as! MyClass
    myObject.property = myValue
    self.managedObjectContext.save()
}

This works fine most of time, but there are some cases where I am getting the object using self.managedObjectContext.objectWithID(objectId) but all it's properties are nil, this should not happen because object is already created and saved in context.

Can some one point me to the reasons of getting object with nil properties value?

The only reason I know is while calling the objectWithID method with temporary objectID, but this is not true in my case as I am using permanent objectID only.

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100

1 Answers1

0

If you are getting nil back for all values that means the object was deleted prior to your most recent access.

Watch the Core Data video from WWDC 2015. This behavior was introduced to avoid issues when you hold onto a reference to a deleted NSManagedObject.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182