0

I have an IOS app with a watch extension. I use a read-only SQLite file with Core Data (which is already populated and not edited).

Accessing the file from the bundle resources works fine using this code.

- (NSURL *)storeURL{
    NSString * databaseFileName = @"db.sqlite";
    return  [NSURL fileURLWithPath: [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: databaseFileName]];
}

However, this code creates a new sqlite file and attach it to the NSPersistentStoreCoordinator:

- (NSURL *)applicationDocumentsDirectory {
    return [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.xxx.xxx"];
}

- (NSURL *)storeURL{
    NSString * databaseFileName = @"db.sqlite";

    NSURL * url = [self applicationDocumentsDirectory];
    NSString * path = [url.absoluteString stringByAppendingPathComponent:databaseFileName];

    path = [path stringByReplacingOccurrencesOfString:@"file:" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, path.length)];

    return [NSURL fileURLWithPath: path];
}

How do I add the SQLite file to a shared app group container in Xcode?

1 Answers1

0

While that was possible in watchOS 1 (since the watch extension used to run on the iPhone), you can no longer use a shared app group to share a Core Data store between the iPhone and Apple Watch.

What you can do is include a copy of that read-only SQLite file in your watchOS app bundle, by adding it to the watch app's target. Then you can use your original storeURL code to add that store to the persistent store coordinator.

enter image description here

The only downside is that the distribution will be larger, since the embedded watch app now contains a separate copy of the SQLite file.

  • Thanks for the detailed answer. I already made that and the watch app runs just fine. The problem that the sqlite file is 178 MB in size. The app will be rejected by the Apple since the max size is 50 MB. – محمد الاشرف أنور May 28 '16 at 16:13
  • I would follow the advice for [this question](http://stackoverflow.com/questions/7856356/how-would-you-minimize-or-compress-core-data-sqlite-file-size) to reduce the size of the file. –  May 28 '16 at 16:35