How to use Realm SDK with iOS App Extensions? #8018
matthewmorek
started this conversation in
General
Replies: 1 comment
-
To share Realm data between your main app and extensions (widgets, intents, etc.), you need to configure a shared App
Group:
### 1. Enable App Groups
In Xcode, for both your main app AND extension targets:
- Signing & Capabilities → + Capability → App Groups
- Create group: `group.com.yourcompany.yourapp`
### 2. Configure Shared Realm Path
```swift
// Shared configuration for both app and extension
struct RealmConfig {
static var shared: Realm.Configuration {
let containerURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourcompany.yourapp")!
let realmURL = containerURL.appendingPathComponent("shared.realm")
var config = Realm.Configuration()
config.fileURL = realmURL
config.schemaVersion = 1
return config
}
}
// Usage in main app
let realm = try! Realm(configuration: RealmConfig.shared)
// Usage in widget/extension - same config
let realm = try! Realm(configuration: RealmConfig.shared)
3. Handle Concurrent Access
Extensions and main app can run simultaneously. Use realm.refresh() in your extension:
// In widget timeline provider
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let realm = try! Realm(configuration: RealmConfig.shared)
realm.refresh() // Get latest data from main app
let data = realm.objects(MyModel.self)
// Build timeline...
}
Important Notes:
- Keep extension Realm usage lightweight (extensions have memory limits)
- Consider using Realm.Configuration.readOnly = true in extensions if you only read data
- Schema must match between app and extension - share your model files
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am developing an app that has a second App Target, an iOS Share Extension, which allows users to add website links to a database (Mongo Atlas) and I use Realm Swift SDK for the main app.
Due to the containerisation of app targets, my main app and the bundled iOS Share Extension use a shared App Group container and can share Realm configuration this way successfully.
Questions
Thanks in advance for help
Beta Was this translation helpful? Give feedback.
All reactions