OliverLetterer / SLRESTfulCoreData

Objc naming conventions, autogenerated accessors at runtime, URL substitutions and intelligent attribute mapping
MIT License
178 stars 13 forks source link

managedObjectCollector prevents life-cycle methods #25

Closed itsthejb closed 10 years ago

itsthejb commented 10 years ago

Hi Oliver,

Think I may have just got to the root of an issue I've been having for quite a while: I have some quite complex entities that need to see up transient properties and various kinds of observation in a reliable fashion, and I'm doing this using the NSManagedObject life-cycle methods, awakeFromInsert, awakeFromFetch, etc.

However, I had been having the issue that these methods weren't being called from the main thread context. They certainly were from the background thread, but not when the object was inserted into the main context.

Just investigating further, in managedObjectCollector()

} else if ([objectIDs isKindOfClass:[NSManagedObjectID class]]) {
    return [context objectWithID:objectIDs];
}

I did my own experimentation and found that using objectWithID: won't call the life-cycle methods if it's called on a main context (for example) when the entity was originally inserted into a child context and that context wasn't first saved. Using existingObjectWithID:error: is more helpful in this case since you get a clear Attempt to access an object not found in store. error. After a save, the life-cycle methods are called as expected.

In the case of SLRESTfulCoreData, I'm not sure the issue is exactly the same, however with the following patch b5cca4790fc262a38d92f306eb0be6c9afd05ead, I seem to get the lifecycle methods triggered as expected.

In any case, please let me know what you think about this.

Thanks!

OliverLetterer commented 10 years ago

I still haven't figured this out yet but I hope to find some time today and check if this introduces any side effects in our own projects. But I think forcing an existing object definitely makes sense and prevents possible bugs.

OliverLetterer commented 10 years ago

I just pushed 1.7.1. Thank You very much :)

itsthejb commented 10 years ago

Great, thanks @OliverLetterer