Skip to content

Commit

Permalink
only update remote on important changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Apr 25, 2023
1 parent 793c8f8 commit 52bb7f0
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Sources/ParseCareKit/ParseRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
self.subscribeToRemoteUpdates = subscribeToRemoteUpdates
if let currentUser = try? await PCKUser.current() {
try Self.setDefaultACL(defaultACL, for: currentUser)
await subscribeToRevisionRecord()
await subscribeToClock()
}
}

Expand Down Expand Up @@ -352,23 +352,30 @@ public class ParseRemote: OCKRemoteSynchronizable {
localClock: OCKRevisionRecord.KnowledgeVector,
completion: @escaping (Error?) -> Void) {
Task {
// 9. Increment and merge clocks if new local revisions were pushed.
var updatedParseVector = parseVector
// 9. Increment and merge clocks if new local revisions were pushed,
// or else simply update thes remote.
if shouldIncrementClock {
updatedParseVector = incrementVectorClock(updatedParseVector)
}
updatedParseVector.merge(with: localClock)

// 10. Save updated clock to the remote and notify peer that sync is complete.
guard let updatedClock = PCKClock.encodeVector(updatedParseVector, for: parseClock) else {
await self.remoteStatus.notSynchronzing()
Logger.pushRevisions.error("Could not encode clock")
completion(ParseCareKitError.couldntUnwrapClock)
return
}

// 10. Save updated clock to the remote and notify peer that sync is complete.
let hasNewerClock = await self.remoteStatus.hasNewerVector(updatedParseVector)
await self.remoteStatus.updateClock(updatedClock)
guard shouldIncrementClock || hasNewerClock else {
await self.remoteStatus.notSynchronzing()
await self.subscribeToClock()
completion(nil)
return
}
do {
await self.remoteStatus.updateClock(updatedClock)
_ = try await updatedClock.save()
Logger.pushRevisions.debug("Finished pushing revisions")
DispatchQueue.main.async {
Expand All @@ -381,7 +388,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
completion(error)
}
await self.remoteStatus.notSynchronzing()
await self.subscribeToRevisionRecord()
await self.subscribeToClock()
}
}

Expand Down Expand Up @@ -422,7 +429,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
}

@MainActor
func subscribeToRevisionRecord() async {
func subscribeToClock() async {
do {
_ = try await PCKUser.current()
guard self.subscribeToRemoteUpdates,
Expand Down

0 comments on commit 52bb7f0

Please sign in to comment.