Skip to content

Commit 6281adb

Browse files
fatbobmanclaude
andcommitted
Update README documentation for limitToInstance parameter and App Group support
- Added limitToInstance parameter documentation in Macro Parameters section - Added comprehensive App Groups and Cross-Process Synchronization section in Important Notes - Explained the problem, solution, and best practices for App Group scenarios - Emphasized the importance of unique prefixes when limitToInstance is false - Added practical examples for main app and widget extension setup - Documented performance considerations for different limitToInstance settings 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e1503b7 commit 6281adb

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,24 @@ You can set parameters directly in the `@ObservableDefaults` macro:
240240
- `prefix`: A prefix for `UserDefaults` keys.
241241
- `autoInit`: Whether to automatically generate the initializer (default is `true`).
242242
- `observeFirst`: Observation priority mode (default is `false`).
243+
- `limitToInstance`: Whether to limit observations to the specific UserDefaults instance (default is `true`). Set to `false` for App Group cross-process synchronization.
243244

244245
```swift
245246
@ObservableDefaults(autoInit: false, ignoreExternalChanges: true, prefix: "myApp_")
246247
class Settings {
247248
@DefaultsKey(userDefaultsKey: "fullName")
248249
var name: String = "Fatbobman"
249250
}
251+
252+
// For App Group cross-process synchronization
253+
@ObservableDefaults(
254+
suiteName: "group.myapp",
255+
prefix: "widget_",
256+
limitToInstance: false
257+
)
258+
class WidgetSettings {
259+
var lastUpdate: Date = Date()
260+
}
250261
```
251262

252263
#### @ObservableCloud Macro Parameters
@@ -593,6 +604,66 @@ class CloudSettings {
593604
- ✅ You're experiencing Swift 6 concurrency compilation errors
594605
- ❌ Your project uses the default `nonisolated` setting (parameter not needed)
595606

607+
### App Groups and Cross-Process Synchronization
608+
609+
When using App Groups to share UserDefaults between your main app and extensions (widgets, app extensions), you need special configuration to ensure proper cross-process notification handling.
610+
611+
#### The Problem
612+
613+
By default, `@ObservableDefaults` only listens to UserDefaults change notifications from its specific UserDefaults instance. When using App Groups:
614+
615+
- Your main app creates: `UserDefaults(suiteName: "group.myapp")`
616+
- Your widget creates: `UserDefaults(suiteName: "group.myapp")`
617+
618+
Even though both access the same data store, they are different object instances. When the widget modifies data, the main app won't automatically receive notifications about the changes.
619+
620+
#### The Solution
621+
622+
Use the `limitToInstance: false` parameter to enable cross-process notifications:
623+
624+
```swift
625+
@ObservableDefaults(
626+
suiteName: "group.com.yourcompany.app",
627+
prefix: "widget_", // IMPORTANT: Use unique prefixes
628+
limitToInstance: false // Enable cross-process notifications
629+
)
630+
class WidgetSettings {
631+
var lastUpdate: Date = Date()
632+
var displayCount: Int = 0
633+
}
634+
```
635+
636+
#### Important: Always Use Unique Prefixes
637+
638+
When `limitToInstance: false`, the macro receives ALL UserDefaults notifications. Without unique prefixes, your app might react to unrelated UserDefaults changes from other parts of your app or different App Groups.
639+
640+
```swift
641+
// Main App
642+
@ObservableDefaults(
643+
suiteName: "group.myapp",
644+
prefix: "main_",
645+
limitToInstance: false
646+
)
647+
class AppSettings {
648+
var userName: String = "User" // Stored as "main_userName"
649+
}
650+
651+
// Widget
652+
@ObservableDefaults(
653+
suiteName: "group.myapp",
654+
prefix: "widget_",
655+
limitToInstance: false
656+
)
657+
class WidgetData {
658+
var userName: String = "Widget" // Stored as "widget_userName"
659+
}
660+
```
661+
662+
#### Performance Considerations
663+
664+
- **Default (`limitToInstance: true`)**: Better performance, only monitors specific instance changes. Use for single-process apps.
665+
- **Cross-Process (`limitToInstance: false`)**: Necessary for App Groups but receives more notifications. Unique prefixes help filter relevant changes.
666+
596667
### General Notes
597668

598669
- **External Changes**: By default, both macros respond to external changes in their respective storage systems.

0 commit comments

Comments
 (0)