You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Either need to make an easier way to do this and/or a related example in the README or the example project. And my first solution of re-querying the object by ID is both inefficient and getting away from the design goals of the framework, I think.
The code from your comment in #1 was the first thing I had tried when I ran into it:
Cd.transact{letobj=try!Cd.create(MyObj.self)
obj.prop =3try!Cd.commit()dispatch_async(dispatch_get_main_queue()){letmyMainThreadObj=Cd.useInCurrentContext(obj)!
// Do something with object}}
But I run into this inside of useInCurrentContext():
guard let originalContext = object.managedObjectContext else{Cd.raise("You cannot transfer an object without a existing context. This object may be transient, or it's original context has been destroyed.")}
I assume because the temporary context for the transaction is gone and so the object has no MOC.
I got it to work using a synchronous dispatch to main - what do you think about potentially keeping that temporary context for the transaction around in the case of heavy-ish work done in the synchronous code on main? If that doesn't seem bad, then I think that's the best route to go. My first concern was with stuff like a race condition with the restoring of existingContext and existingNoCommit, but I guess those are protected because that thread is blocked until the newWriteContext.preformBlockAndWait is done. This is what I'm using now:
Cd.transact{letobj=try!Cd.create(MyObj.self)
obj.prop =3try!Cd.commit()dispatch_sync(dispatch_get_main_queue()){// <-- SYNCHRONOUSletmyMainThreadObj=Cd.useInCurrentContext(obj)!
// Do something with object}}
The text was updated successfully, but these errors were encountered:
I think I might be too heavy-handed with the guard against transferring w/o an existing context. If the NSManagedObject has a permanent objectID I should let it pass as well. I think this would make the async version work, but will test later.
In fact, I should probably be checking against temporary IDs, but I think the check on originalContext.hasChanges is doing that implicitly (since any context w/ an object that isn't saved will have changes..)
Ok, 0.11.1 is up with the change to context-checking on useInCurrentContext. Now any object with a persistent ID should be useable; still relying on the hasChanges flag to catch transient objects though.
Also added some more content to the readme about this, as well as notes on the update handler.
Either need to make an easier way to do this and/or a related example in the README or the example project. And my first solution of re-querying the object by ID is both inefficient and getting away from the design goals of the framework, I think.
The code from your comment in #1 was the first thing I had tried when I ran into it:
But I run into this inside of
useInCurrentContext()
:I assume because the temporary context for the transaction is gone and so the object has no MOC.
I got it to work using a synchronous dispatch to main - what do you think about potentially keeping that temporary context for the transaction around in the case of heavy-ish work done in the synchronous code on main? If that doesn't seem bad, then I think that's the best route to go. My first concern was with stuff like a race condition with the restoring of
existingContext
andexistingNoCommit
, but I guess those are protected because that thread is blocked until thenewWriteContext.preformBlockAndWait
is done. This is what I'm using now:The text was updated successfully, but these errors were encountered: