Sharing core and data stack between apps and extensions in iOS8

I created framework

to share my data objects between Application and extension. This includes data model and sqlite file with my Core Data

db.

I am concerned about what might happen if both applications and the extension try to access this shared SQL db block.

What can go wrong if the application makes some changes to the db in the background while the extension is using it?

What's the best practice in this case?

+3


source to share


1 answer


As @CL notes, SQLite is fine with this. But you are not using SQLite directly, so you may need to do some basic work in the data layer to maintain consistency. With an iOS app and extension, you have two separate processes that can make changes to data. Your code should take this into account.

If your application's extension only displays data (for example, a "today's" extension that only displays data generated in the application), you probably don't need to do anything. If your application is running in the background and generating new data while the extension is visible, the extension's data may be slightly outdated. If it's important, you can update it. But today, extensions are usually not visible for very long, so it probably isn't worth it. In this case, I would only use NSReadOnlyPersistentStoreOption

Core Core when setting up the data stack to clearly state intent and prevent unintended changes.



If your application extension creates new data or modifies existing data, your application needs to be aware of this and respond accordingly. What changes you make will depend on how the extension handles shared data. For data that has changed, your application probably needs to call refreshObject:mergeChanges:

for any memory-managed objects with NO

as the second argument. It is also likely that any selections need to be redone where changes might affect the search predicate. This will ensure that you receive the latest updates. For new data, you will need to re-fetch whatever data the app is working with in order to receive new additions / deletions. A good time to check would be when the application comes to the fore (i.e. when UIApplicationWillEnterForegroundNotification

dispatched).

+4


source







All Articles