Master data (magic recording) + WatchKit extension + Cocoa Touch Framework

That's what I'm doing:

Now i have

  • IPhone app
  • WatchKit Extension
  • A Cocoa Touch Framework that contains all my common classes

What I would like to accomplish has a persistent storage (master data), which is shared between my iPhone application and WatchKit Extension.

So this is what I have done so far

  • Create an application group to have a common container.
  • Add a basic data model (Model.xcdatamodeld) to my Touch Cocoa construct.
  • One entity created in this model.
  • Subclassed NSMangedObject for this object and added to my Cocoa Touch Framework
  • Added DataManager class for my Cocoa Touch Framework

This is 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() } } 

Problem i am facing

When I launch my DataManager from the AppDelegate app for iPhone, no crash occurs, but station will be null.

When replacing the last line with let stations: [Station]? = Station.MR_findAll() as? [Station] let stations: [Station]? = Station.MR_findAll() as? [Station] let stations: [Station]? = Station.MR_findAll() as? [Station] application crashes and displays the following error: A fetch request must have an entity.

I searched all the SO and Magical Records issues on GitHub but couldn't find anything to push me in the right direction. All help is much appreciated.

+7
cocoa-touch swift watchkit core-data magicalrecord
source share
2 answers

Thanks to a comment by Leo Natan Now I understand that I have to store my main data both in my iPhone sandbox and in my WatchKit application sandbox. And not inside a shared container like I tried.

When creating for Watch OS 2, I can use the WatchKit Connectivity Framework to synchronize both databases. Meanwhile, I could use a solution like MMWormhole to achieve the same.

0
source share

I completed my task in my direct application with a clock and iPhone. I do not need 2 stores. and MMWormhole is good to help instantly call back both sides. I also handled the handleWatchKitExtensionRequest. view handleWatchKitExtensionRequest.

Synchronization is correct and works well.

I followed this forum. - http://www.makeandbuild.com/blog/post/watchkit-with-shared-core-data

Hope this can help you.

0
source share

All Articles