jmfieldman / cadmium Goto Github PK
View Code? Open in Web Editor NEWA Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.
License: MIT License
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.
License: MIT License
Hi there. Been seeing how the library feels in a project, and after getting used to the way you've guided things so far (my last bump was on how to use an object created in a transaction in a dispatch to main - ended up having to get the objectID
and re-querying for it in the main thread), I've run up against something I can't figure out.
Cd.transact {
let txObj = Cd.useInCurrentContext(self.fetchedResultsController.objectAtIndexPath(indexPath) as! CdManagedObject)!
Cd.delete(txObj)
}
I get the exception:
*** Terminating app due to uncaught exception 'Cadmium Exception', reason: 'You cannot modify a managed object on the main thread. Only from inside a transaction.'
It's happening starting in NSManagedObjectContextDidSaveNotification
:
dispatch_async(dispatch_get_main_queue()) {
_mainThreadContext?.mergeChangesFromContextDidSaveNotification(notification) // <-- HERE
}
And then raising in CdManagedObject
's willChangeValueForKey()
, on the only relationship property I have in this class. e.g.
extension MyObj {
@NSManaged var text: String
@NSManaged var relationship: MyOtherObj?
}
#0 0x00000001001a01bc in CdManagedObject.willChangeValueForKey(String) -> () at Cadmium/CdManagedObject.swift:123
#1 0x00000001001a05e8 in @objc CdManagedObject.willChangeValueForKey(String) -> () ()
#2 0x00000001833406c8 in _sharedIMPL_setvfk_core ()
#3 0x000000018336978c in -[NSManagedObject(_NSInternalMethods) _propagateDelete:] ()
#4 0x00000001833b4744 in -[NSManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:] ()
#5 0x00000001833b4c20 in -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] ()
#6 0x00000001001a41c0 in static CdManagedObjectContext.(initializeMasterContexts(coordinator : NSPersistentStoreCoordinator?) -> ()).(closure #1).(closure #1) at Cadmium/CdManagedObjectContext.swift:76
Any thoughts? Thanks!
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 {
let obj = try! Cd.create(MyObj.self)
obj.prop = 3
try! Cd.commit()
dispatch_async(dispatch_get_main_queue()) {
let myMainThreadObj = 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 {
let obj = try! Cd.create(MyObj.self)
obj.prop = 3
try! Cd.commit()
dispatch_sync(dispatch_get_main_queue()) { // <-- SYNCHRONOUS
let myMainThreadObj = Cd.useInCurrentContext(obj)!
// Do something with object
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.