CoreStore
CoreStore copied to clipboard
Sharing data with a widget extension
I'm creating a widget to acompany my app but it seems like that the extension does not speak to the same DB as the app instance. is there any guide to setting the app group or something so that I can set the data in app for the widget extension to read?
I'm planning to fully support this feature in a future release, but for now you should still be able to do this by manually managing your app-group's folder and passing it to SQLiteStore.init(fileURL:...)
. Something like
let dataStack = DataStack(...)
// ...
dataStack.addStorage(
SQLiteStore(
fileURL: FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.YourApp")
.appendingPathComponent("myDB.sqlite"), // Note that ideally you should put this in some containing folder and generate that directory yourself with createDirectory()
// ...
),
// ...
)
Do note that even if this does work, PersistentHistoryTracking is currently not implemented in CoreStore, so if you need real-time change observation you will need to implement that on your own. (ref: https://www.avanderlee.com/swift/core-data-app-extension-data-sharing/) If you decide to do so, CoreStore does have a DataStack.refreshAndMergeAllObjects()
method you can use to force refetching on all objects.
Thanks for that! I was unsure how to solve this particular issue but that worked like a charm
@gardyna Glad it worked! I'm keeping this ticket open as a feature-request ticket, and so that others who have the same question may find it