Skip to content

Commit 78b895d

Browse files
fatbobmanclaude
andcommitted
Clarify prefix usage in App Group documentation
- Remove widget-specific prefix examples that implied different prefixes per process - Emphasize that limitToInstance: false listens to ALL system UserDefaults notifications - Clarify that prefix acts as a filter to respond only to intended suiteName changes - Use better examples showing different App Groups rather than same-group processes - Update performance considerations to reflect system-wide notification listening This addresses the misconception that different processes in the same App Group need different prefixes, when actually they typically share the same prefix and the prefix serves to isolate different suites. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6281adb commit 78b895d

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

README.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ class Settings {
252252
// For App Group cross-process synchronization
253253
@ObservableDefaults(
254254
suiteName: "group.myapp",
255-
prefix: "widget_",
255+
prefix: "myapp_",
256256
limitToInstance: false
257257
)
258-
class WidgetSettings {
258+
class SharedSettings {
259259
var lastUpdate: Date = Date()
260260
}
261261
```
@@ -624,45 +624,53 @@ Use the `limitToInstance: false` parameter to enable cross-process notifications
624624
```swift
625625
@ObservableDefaults(
626626
suiteName: "group.com.yourcompany.app",
627-
prefix: "widget_", // IMPORTANT: Use unique prefixes
627+
prefix: "myapp_", // IMPORTANT: Use a unique prefix
628628
limitToInstance: false // Enable cross-process notifications
629629
)
630-
class WidgetSettings {
630+
class SharedSettings {
631631
var lastUpdate: Date = Date()
632632
var displayCount: Int = 0
633633
}
634634
```
635635

636-
#### Important: Always Use Unique Prefixes
636+
#### Critical: Always Use a Unique Prefix
637637

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.
638+
When `limitToInstance: false`, the macro listens to ALL UserDefaults change notifications from the entire system, not just your specific suite. This means it will receive notifications from:
639+
640+
- `UserDefaults.standard`
641+
- Other App Groups (`group.otherapp`)
642+
- Any other UserDefaults instances in your app
643+
644+
**The prefix acts as a filter** to ensure your class only responds to changes from your intended suiteName:
639645

640646
```swift
641-
// Main App
647+
// App Group suite
642648
@ObservableDefaults(
643649
suiteName: "group.myapp",
644-
prefix: "main_",
650+
prefix: "myapp_", // Only respond to keys starting with "myapp_"
645651
limitToInstance: false
646652
)
647-
class AppSettings {
648-
var userName: String = "User" // Stored as "main_userName"
653+
class AppGroupSettings {
654+
var sharedData: String = "data" // Stored as "myapp_sharedData"
649655
}
650656

651-
// Widget
657+
// Different App Group suite
652658
@ObservableDefaults(
653-
suiteName: "group.myapp",
654-
prefix: "widget_",
659+
suiteName: "group.anotherapp",
660+
prefix: "anotherapp_", // Only respond to keys starting with "anotherapp_"
655661
limitToInstance: false
656662
)
657-
class WidgetData {
658-
var userName: String = "Widget" // Stored as "widget_userName"
663+
class AnotherAppSettings {
664+
var sharedData: String = "other" // Stored as "anotherapp_sharedData"
659665
}
660666
```
661667

668+
Without unique prefixes, your `AppGroupSettings` might incorrectly react to changes from `group.anotherapp` or `UserDefaults.standard`.
669+
662670
#### Performance Considerations
663671

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.
672+
- **Default (`limitToInstance: true`)**: Better performance, only monitors changes from the specific UserDefaults instance. Recommended for single-process apps.
673+
- **Cross-Process (`limitToInstance: false`)**: Necessary for App Groups but receives ALL system UserDefaults notifications. The prefix is essential to filter only relevant changes from your target suite.
666674

667675
### General Notes
668676

0 commit comments

Comments
 (0)