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
I just implemented a today widget in one of my apps. I needed to read a preference item in the today widget that was currently stored in my main app. Usually, this would mean having to migrate the key over to the App Groups user defaults suite. However, I'm lazy and instead just went with observing the key and then setting it in the App Groups user defaults suite when changed:
// Constants.swiftextensionDefaults.Keys{staticlettimeZonesInMenu=Key<[ChosenTimeZone]>("timeZonesInMenu", default:[])}// SharedConstants.swiftstructConstants{staticletappGroupId="XXXX.com.sindresorhus.Dato.shared"staticletsharedDefaults=UserDefaults(suiteName: appGroupId)!
}extensionDefaults.Keys{staticletsharedTimeZones=Key<[ChosenTimeZone]>("sharedTimeZones", default:[], suite:Constants.sharedDefaults)}// OtherFile.swift// Sync time zone changes to the shared item.Defaults.observe(.timeZonesInMenu){ change inDefaults[.sharedTimeZones]= change.newValue
}.tieToLifetime(of:self)
And I was thinking this might be useful for others too.
I'm thinking:
// Key with same nameDefaults.mirror(.key, to: suite)// Key with different name (where the key is strongly-typed and defined in a shared Constants.swift file or something)Defaults.mirror(.key, to: suite, as:.sharedKey)// Key with different nameDefaults.mirror(.key, to: suite, as:"sharedKey")
Would probably need a similar unmirror method too.
Note that this is not syncing. It's only one way.
Feedback welcome.
The text was updated successfully, but these errors were encountered:
I just implemented a today widget in one of my apps. I needed to read a preference item in the today widget that was currently stored in my main app. Usually, this would mean having to migrate the key over to the App Groups user defaults suite. However, I'm lazy and instead just went with observing the key and then setting it in the App Groups user defaults suite when changed:
And I was thinking this might be useful for others too.
I'm thinking:
Would probably need a similar
unmirror
method too.Note that this is not syncing. It's only one way.
Feedback welcome.
The text was updated successfully, but these errors were encountered: