Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Cannot init the PersistentContainer #179

Open
YLKwan opened this issue Aug 19, 2018 · 5 comments
Open

Cannot init the PersistentContainer #179

YLKwan opened this issue Aug 19, 2018 · 5 comments

Comments

@YLKwan
Copy link

YLKwan commented Aug 19, 2018

let persistentContainer = PersistentContainer()

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: AlecrimCoreData.PersistentContainerError.invalidManagedObjectModelURL

cap

@vmartinelli
Copy link
Member

vmartinelli commented Aug 19, 2018

Hello.

The framework is trying to infer the Core Data model URL using the framework name itself (AlecrimCoreData in this case). This code was made thinking about subclasses.

I think passing your Core Data model filename (without extension) as a constructor parameter may work. If not, let me know.

@YLKwan
Copy link
Author

YLKwan commented Aug 20, 2018

    public convenience init(name: String? = nil) {
        try! self.init(name: name, managedObjectModel: type(of: self).managedObjectModel(), storageType: .disk, persistentStoreURL: try! type(of: self).persistentStoreURL(), persistentStoreDescriptionOptions: nil, ubiquitousConfiguration: nil)
    }

The convenience 'init' method does not passing the 'name' parameter to 'managedObjectModel' method.

    public static func managedObjectModel(withName name: String? = nil, in bundle: Bundle? = nil) throws -> NSManagedObjectModel {
        let bundle = bundle ?? Bundle(for: Self.self)
        let name = name ?? bundle.bundleURL.deletingPathExtension().lastPathComponent

        let managedObjectModelURL = try self.managedObjectModelURL(withName: name, in: bundle)

        guard let managedObjectModel = NSManagedObjectModel(contentsOf: managedObjectModelURL) else {
            throw PersistentContainerError.managedObjectModelNotFound
        }

        return managedObjectModel
    }

And in the 'managedObjectModel' method, it using the bundle of AlecrimCoreData Framework but not the Application bundle.

Should I subclass the PersistentContainer before using it?

@kvacquier
Copy link

Same here, do you have any idea ?

@vmartinelli
Copy link
Member

I'm going through personal problems, sorry for the delay in answering.

@YLKwan: Subclassing the PersistentContainer class may work, using a constructor like this:

public init() throws {
    let managedObjectModel = ...
    let persistentStoreURL = ...

    try super.init(managedObjectModel: managedObjectModel, storageType: .disk, persistentStoreURL: persistentStoreURL)
}

@kvacquier: A future update should improve how this is done in the framework.

@YLKwan
Copy link
Author

YLKwan commented Sep 19, 2018

try using this extension for temporary fix

import AlecrimCoreData
extension PersistentContainer {

	public convenience init(name: String? = nil) {
		try! self.init(name: name, managedObjectModel: type(of: self).managedObjectModel(withName: name, in: Bundle.main), storageType: .disk, persistentStoreURL: try! type(of: self).persistentStoreURL(), persistentStoreDescriptionOptions: nil, ubiquitousConfiguration: nil)
	}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants