How can I tell whether an `NSManagedObject` has been deleted?

前端 未结 5 1563
[愿得一人]
[愿得一人] 2020-11-27 11:06

I have an NSManagedObject that has been deleted, and the context containing that managed object has been saved. I understand that isDeleted returns

5条回答
  •  清歌不尽
    2020-11-27 11:35

    I fear the discussion in the other answers is actually hiding the simplicity of the correct answer. In pretty much all cases, the correct answer is:

    if ([moc existingObjectWithID:object.objectID error:NULL])
    {
        // object is valid, go ahead and use it
    }
    

    The only cases this answer doesn't apply in is:

    1. If you are targetting Mac OS 10.5 or earlier
    2. If you are targetting iOS 2.0 or earlier
    3. If the object/context has not been saved yet (in which case you either don't care because it won't throw a NSObjectInaccessibleException, or you can use object.isDeleted)

提交回复
热议问题