Here's what I'm up to:
I now have
- An iPhone app
- A WatchKit Extension
- A Cocoa Touch Framework that holds all my shared classes
What I would like to accomplish, is having a persistent storage (Core Data) that is shared between my iPhone app and WatchKit Extension.
So this is what I've done so far
- Create an app group to have a shared container.
- Add a Core Data Model (Model.xcdatamodeld) to my Cocoa Touch Framework.
- Created one Entity in this model
- Created an NSMangedObject subclass for this entity and added is to my Cocoa Touch Framework
- Added a DataManager class to my Cocoa Touch Framework
Here what the initializer in my DataManager looks like
public init() {
let sharedContainerURL: NSURL? = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.company.Project")
if let sharedContainerURL = sharedContainerURL {
let storeURL = sharedContainerURL.URLByAppendingPathComponent("Model.sqlite")
MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStoreAtURL(storeURL)
let station: Station? = Station.MR_createEntity()
}
}
The issue I'm running into
When I init my DataManager from the iPhone app's AppDelegate, no crash occures, but station
will be nil.
When I replace the last line with let stations: [Station]? = Station.MR_findAll() as? [Station]
the app crashes and shows the following error: A fetch request must have an entity.
I've searched all of SO and Magical Records issues on GitHub, but couldn't find anything to push me in the right direction. All help is much appreciated.