JohnEstropia / CoreStore

Unleashing the real power of Core Data with the elegance and safety of Swift
MIT License
4k stars 254 forks source link

Sharing data with a widget extension #408

Open gardyna opened 3 years ago

gardyna commented 3 years ago

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?

JohnEstropia commented 3 years ago

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.

gardyna commented 3 years ago

Thanks for that! I was unsure how to solve this particular issue but that worked like a charm

JohnEstropia commented 3 years ago

@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

imrobbyrc commented 3 months ago

maybe can you implement this tickets? as i need this to in my notification extension