Search code examples
watchkitmagicalrecord-2.2

WatchKit and MagicalRecord - is that possible?


I've got an iOS app with Core Data via MagicalRecord. Now I'd like to extend the app with a WatchKit Extension and therefore need access to MagicalRecord. Is this possible or do I have to switch over to "native" Core Data? It would be perfect if I could use Magical Record in the WatchKit extension, but I couldn't figure out how until now. I can't even include my cocoapods in the shared Cocoa Touch Framework... (use CocoaPods 0.36.0 in CocoaTouchFramework with Swift)


Solution

  • There shouldn't be any problems using MagicalRecord in your WatchKit Extension. You will need to do the following.

    1. In your General Settings for your WatchKit Extension you will need to add libPods.a to your Linked Frameworks and Libraries.

    enter image description here

    enter image description here

    1. You will need to have MagicalRecord used a database in the shared group folder and not its default app folder. To do that you will need to do this when you set up MagicalRecord

      NSString* dbPath = [self sharedAppGroupDirectory];
      dbPath = [dbPath stringByAppendingPathComponent:@"MyDatabaseName.sqlite"];
      NSURL* dbURL = [NSURL fileURLWithPath:dbPath];
      [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)dbURL];
      

    You can get the shared App Group Directory with this.

    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *appGroupName = @"group.com.mygroup";
    NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
    NSString* path = [groupContainerURL filePathString];