Tiago Lira
Tiago Lira

Reputation: 2550

iOS App groups - shared CoreData fails to open

I have an app with a CoreData database and access it using UIManagedDocument. To share the data with the Watchkit app, I'm moving the database to a shared App Group.

The store is created correctly it seems, but after that it often becomes impossible to access it: the UIManagedDocument keeps trying to reconfigure the store, and NSFileManager doesn't find the file (with fileExistsAtPath).

The problem is in the database location - if I save it in the documents directory instead of the shared Group, it works without trouble. It's also not a problem of accessing it from both apps, even if I don't open the watch app at all the problem appears. The group is also working correctly for sharing NSUserDefaults.

Did anyone see similar problems? Is there any locking mechanism that I need to deal with in a specific way? Any help will be greatly appreciated, I've been struggling a lot with this issue!

Upvotes: 0

Views: 289

Answers (1)

Tiago Lira
Tiago Lira

Reputation: 2550

After a lot of searching, I found that the storeURL would change (randomly) between these two:

file:///var/mobile/...
file:///private/var/mobile/...

I used URLByResolvingSymlinksInPath on the URL as specified in the following answer, and incredibly, it fixed it: What does the /private prefix on an iOS file path indicate?

I'm assuming this is a system bug with the App Group paths... it seems that the different paths were causing the persistentStoreCoordinator to try to reopen the store when it was already open.

Upvotes: 2

Related Questions