Comments (9)
Also, as the device B is newly installed, the initial value timestamp will be latest than the iCloud's, so your compare will fail.
The local timestamp will be updated only when it receives the remote update from iCloud or we change the value in UserDefaults
. Since device B is newly installed, the initial value timestamp will not exist. For more details, please feel free to read the source code :).
The initial value should never be send. Because without sending it, all device have been already the same as being synced.
I agree with this. However, it will require that the Defaults Value
conforms to Equatable
to check whether the current value is the initial value.
from defaults.
@hank121314 I have confirmed that this is a problem. If I have a Bool
key with default value of false
, run the app on device A, toggle it to true
. Then I run the app on device B. Device A will then have the toggle changed to false
again. This is the wrong behavior. The initial value should not override the user set value.
from defaults.
I made a quick example app to test the syncing: Example app.zip
from defaults.
I guess this issue is because the value from device A(which is true
) has not synced to iCloud storage when device B starts.
When device B starts, it cannot find the value in iCloud storage, so it will also sync the default value, which is false
, to device A.
If the value from device A has already synced to iCloud storage, then when device B starts, it should also have the value true
(due to iCloud.add
, syncWithoutWaiting(key)
will be triggered, using remote storage as the data source).
from defaults.
I guess this issue is because the value from device A(which is
true
) has not synced to iCloud storage when device B starts. When device B starts, it cannot find the value in iCloud storage, so it will also sync the default value, which isfalse
, to device A. If the value from device A has already synced to iCloud storage, then when device B starts, it should also have the valuetrue
(due toiCloud.add
,syncWithoutWaiting(key)
will be triggered, using remote storage as the data source).
Not correct.
This issue is because iCloud always accept the new value as the latest value. So you should not send a value that should not be sent.
For device A, though true was sent to iCloud. But when B starts, B send false, which is initial to iCloud, so false is the new latest and be sent to A from iCloud.
The initial value should never be send. Because without sending it, all device have been already the same as being synced. So it is unnecessary to sync them. Apple knows that, that why all .onChange new API with initial to false as default. On the other hand, Defaults use true as default in legacy, so it may need to change or at least have an option.
from defaults.
For device A, though true was sent to iCloud. But when B starts, B send false, which is initial to iCloud, so false is the new latest and be sent to A from iCloud.
I understand what you mean, but in the internal Defaults
, we use a timestamp to compare which value is more recent between the UserDefaults
and iCloud storage. So when Device B starts, it first fetches the remote timestamp from iCloud and compares it with the local one. Since it is a newly installed app, the local timestamp will be empty. If the remote timestamp is greater or the local timestamp does not exist, we will update the value from remote to local instead.
from defaults.
For device A, though true was sent to iCloud. But when B starts, B send false, which is initial to iCloud, so false is the new latest and be sent to A from iCloud.
I understand what you mean, but in the internal
Defaults
, we use a timestamp to compare which value is more recent between theUserDefaults
and iCloud storage. So when Device B starts, it first fetches the remote timestamp from iCloud and compares it with the local one. Since it is a newly installed app, the local timestamp will be empty. If the remote timestamp is greater or the local timestamp does not exist, we will update the value from remote to local instead.
Since it is not need to send the initial value to iCloud at all. Why send it at the first place? Without sending it, you even no need to check the timestamp as iCloud can do all things perfect.
Also, as the device B is newly installed, the initial value timestamp will be latest than the iCloud's, so your compare will fail.
from defaults.
I agree with this. However, it will require that the Defaults Value conforms to Equatable to check whether the current value is the initial value.
Can we not use UserDefaults.standard.dictionaryRepresentation().keys.contains()
to check whether the key has been set?
from defaults.
Alternatively, we could locally track when a key is set locally the first time.
from defaults.
Related Issues (20)
- Support external storage option?
- Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates. HOT 1
- Default value is not stored, which means the read value can change unexpectedly. HOT 1
- High CPU and Hign Memory increase HOT 2
- Support `Color.Resolved`
- Observer not being triggered when using `defaults write` at the command line HOT 1
- Crashes related to KVO and UserDefaults HOT 7
- Improve the API for choosing the type of serialization HOT 10
- Error occured when generating privacy reports. HOT 1
- Sendable conformance for Key
- Privacy Manifest HOT 1
- `isOn` as parameter for toggle initializers HOT 1
- Improve `UserDefaultsKeyObservation`
- "Cannot find 'OSAllocatedUnfairLock' in scope" when archiving HOT 3
- 9.0 beta: Color.Resolved not working on watchOS 10
- Mocking
- Optional Keys crash Xcode 16 HOT 7
- -[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSString' (0x1e1552fd8) [/System/Library/Frameworks/Foundation.framework]' HOT 2
- My Defaults.Key is dynamically created, how to use the @Default syntax. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from defaults.