Sharing data with a widget extension #351

Open
opened 2025-12-29 15:29:46 +01:00 by adam · 4 comments
Owner

Originally created by @gardyna on GitHub (Nov 17, 2020).

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?

Originally created by @gardyna on GitHub (Nov 17, 2020). 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?
adam added the enhancementquestion labels 2025-12-29 15:29:46 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Nov 18, 2020):

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.

@JohnEstropia commented on GitHub (Nov 18, 2020): 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.
Author
Owner

@gardyna commented on GitHub (Nov 18, 2020):

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

@gardyna commented on GitHub (Nov 18, 2020): Thanks for that! I was unsure how to solve this particular issue but that worked like a charm
Author
Owner

@JohnEstropia commented on GitHub (Nov 18, 2020):

@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

@JohnEstropia commented on GitHub (Nov 18, 2020): @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
Author
Owner

@imrobbyrc commented on GitHub (Jul 3, 2024):

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

@imrobbyrc commented on GitHub (Jul 3, 2024): maybe can you implement this tickets? as i need this to in my notification extension
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#351