Coder Social home page Coder Social logo

jmfieldman / cadmium Goto Github PK

View Code? Open in Web Editor NEW
122.0 122.0 5.0 1.26 MB

A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.

License: MIT License

Objective-C 3.50% Swift 94.43% Ruby 1.01% C 1.06%

cadmium's People

Contributors

jmfieldman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cadmium's Issues

Can't seem to delete object with a relationship from a fetched results controller

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!

Quirks with doing simple task of creating object and then using on main thread as soon as possible

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
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.