Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable syncing from local if its default after iCloud.add #185

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

hank121314
Copy link
Collaborator

@hank121314 hank121314 commented Aug 5, 2024

Description

This PR fixes #172.

By default, Defaults will create a sync task that automatically detects the data source using timestamps. However, this might cause an issue if the device is newly installed, as it might accidentally push the default value to iCloud. It's also unnecessary to publish the default value to iCloud upon initialization, since all devices should already have that value.

Implementation

Added a new function isDefaultValue for Defaults.Key<Value>.
To detect if the locally stored value is a default value, I had to change Defaults.iCloud.add to use a generic packed parameter.

Question

This change introduces a new issue: we cannot support Defaults.iCloud.add with an array of Defaults.Key<Value> due to the lack of variadic parameter support. Is there any way to resolve this?

@hank121314 hank121314 requested a review from sindresorhus August 5, 2024 12:26
/**
Check whether the stored value is the default value.
*/
public func isDefaultValue() -> Bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a property and I don't think it should be public as it wouldn't work for non-equatable values.

We can instead add a version of this publicly:

extension Default where Value: Equatable {
/**
Indicates whether the value is the same as the default value.
*/
public var isDefaultValue: Bool { wrappedValue == defaultValue }
}

Sources/Defaults/Utilities.swift Show resolved Hide resolved
public static func add(_ keys: [Defaults.Keys]) {
synchronizer.add(keys)
public static func add<each Value: Defaults.Serializable>(_ keys: repeat Defaults.Key<each Value>) {
repeat synchronizer.add(each keys)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untested and would require Swift 6, but maybe:

public static func add<each Value: Defaults.Serializable>(_ keys: repeat [Defaults.Key<each Value>]) {
	for key in repeat each keys {
		add(key)
	}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will add a TODO for it first!

@sindresorhus sindresorhus merged commit 174b791 into sindresorhus:main Aug 12, 2024
2 checks passed
@sindresorhus
Copy link
Owner

Looks great. Thanks for fixing this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question on 9.0 beta iCloud Syncing
2 participants