magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.8k forks source link

Is it possible to load model from framework? #1290

Open lolgear opened 7 years ago

lolgear commented 7 years ago

It is a swift time today. All parts of app are supposed to be 'frameworkable', because 'targets' should be 'cleaned' from massive view controllers and other code.

Database embedded from not an exception in this world. All models live in separated framework ( Database.framework, for example ). And also xcdatamodel lives with them.

What is the point? How to setup this kind of stack?

I ended up with this:

        guard let bundle = Bundle(identifier: "com.opensource.Database") else {
            return
        }

        let databaseName = "Database"
        guard let databaseUrl = bundle.url(forResource: databaseName, withExtension: "momd") else {
            return
        }

        guard let stack = MagicalRecord.setupAutoMigratingStack(withSQLiteStoreNamed: "Database") else {
            return
        } 

        stack.model = NSManagedObjectModel(at: databaseUrl)

But it doesn't work.

lolgear commented 7 years ago

works:

    func setup() {
        // pass scheme name to it.
        guard let bundle = Bundle(identifier: "com.opensource.Database") else {
            return
        }

        guard let databaseUrl = bundle.url(forResource: "Database", withExtension: "momd") else {
            return
        }

        guard let stack = MagicalRecord.setupAutoMigratingStack() else {
            return
        }

        stack.model = NSManagedObjectModel(at: databaseUrl)
        stack.coordinator = stack.createCoordinator(options: nil)
    }

nope :/

1038