refactor(ios): encode persistence state in Loadable enum, throw on save#1261
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #1260. That PR closed the bug chain that wiped iCloud-synced connections after the build 12 to 13 update. This PR cleans up the three architecture smells that fix carried over, without changing user-visible behavior.
Smells fixed
persistenceIntegrityflag ran alongside the data arrays. Two fields described one concept ("is the load valid?"), and the initial.okvalue lied for the brief window beforeloadPersistedData()ran. Replaced with aLoadable<T>enum (.loading | .loaded(T) | .failed(Error)) used internally for connections, groups, and tags. A computedloadStatus: LoadStatusexposes the combined state. Views still read flat[Connection]/[Group]/[Tag]arrays as before; the load state is encoded in the storage itself.save()plus blind integrity flip. The old callbacks setpersistenceIntegrity = .okafter asave()that swallowed every error throughtry?.ConnectionPersistence,GroupPersistence, andTagPersistencenow havesave() throws. The singlepersist(connections:)/persist(groups:)/persist(tags:)bridge insideAppStateis the only place that writes; it transitions theLoadablestate and logs save failures via OSLog. State and disk no longer drift silently.if !remoteChanges.changedConnections.isEmpty || ... { onConnectionsChanged?(merged) }) put the "is this worth saving?" check in the wrong layer. Moved to the receiver: each callback inAppStatenow diffsmerged != self.connectionsbefore callingpersist. Coordinator fires callbacks unconditionally; the persistence layer decides whether to commit.Other small wins
getCurrentStatereturns optional; sync skips whenloadStatus != .ready..loading, not a flag that claims.okbefore any load attempts.osimports and dead loggers fromGroupPersistenceandTagPersistenceafter theirsavelost the swallowed-error path.No new strings, no new public types, no behavior change visible from any view.
Test plan
.active.runBackgroundSynclogs "Background sync skipped: persistence load not ready" and bails before touching anything.connections.json— load logs the error, sidebar shows the empty state, Refresh from iCloud recovers the data and load state transitions to.ready.swiftlint lint --strictpasses (0 violations).