You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: README.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,13 +240,24 @@ You can set parameters directly in the `@ObservableDefaults` macro:
240
240
-`prefix`: A prefix for `UserDefaults` keys.
241
241
-`autoInit`: Whether to automatically generate the initializer (default is `true`).
242
242
-`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.
- ✅ You're experiencing Swift 6 concurrency compilation errors
594
605
- ❌ Your project uses the default `nonisolated` setting (parameter not needed)
595
606
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
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
+
classAppSettings {
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
+
classWidgetData {
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
+
596
667
### General Notes
597
668
598
669
-**External Changes**: By default, both macros respond to external changes in their respective storage systems.
0 commit comments