diff --git a/Sources/ParseCareKit/Models/RemoteSynchronizing.swift b/Sources/ParseCareKit/Models/RemoteSynchronizing.swift index 00e65c140..d78383d30 100644 --- a/Sources/ParseCareKit/Models/RemoteSynchronizing.swift +++ b/Sources/ParseCareKit/Models/RemoteSynchronizing.swift @@ -25,13 +25,13 @@ actor RemoteSynchronizing { knowledgeVector = vector } - func hasNewerRevision(_ vector: OCKRevisionRecord.KnowledgeVector) -> Bool { + func hasNewerRevision(_ vector: OCKRevisionRecord.KnowledgeVector, for uuid: UUID) -> Bool { guard !isSynchronizing else { return false } - guard let knowledgeVector = knowledgeVector else { + guard let currentClock = knowledgeVector?.clock(for: uuid) else { return true } - return vector >= knowledgeVector + return vector.clock(for: uuid) > currentClock } } diff --git a/Sources/ParseCareKit/ParseRemote.swift b/Sources/ParseCareKit/ParseRemote.swift index 47dafac81..e9379046b 100644 --- a/Sources/ParseCareKit/ParseRemote.swift +++ b/Sources/ParseCareKit/ParseRemote.swift @@ -201,7 +201,7 @@ public class ParseRemote: OCKRemoteSynchronizable { do { let updatedVector = try PCKClock.decodeVector(updatedClock) Task { - guard await self.remoteStatus.hasNewerRevision(updatedVector) else { + guard await self.remoteStatus.hasNewerRevision(updatedVector, for: self.uuid) else { return } self.parseDelegate?.didRequestSynchronization(self) @@ -349,7 +349,7 @@ public class ParseRemote: OCKRemoteSynchronizable { return } - guard await !self.remoteStatus.hasNewerRevision(parseVector) else { + guard await !self.remoteStatus.hasNewerRevision(parseVector, for: self.uuid) else { let errorString = "New knowledge on server. Pull first then try again" Logger.pushRevisions.error("\(errorString)") await self.remoteStatus.notSynchronzing() @@ -412,8 +412,7 @@ public class ParseRemote: OCKRemoteSynchronizable { updatedParseVector = incrementVectorClock(updatedParseVector) } updatedParseVector.merge(with: localClock) - guard (shouldIncrementClock || (!shouldIncrementClock && updatedParseVector.uuids.count > parseVector.uuids.count)), - let updatedClock = PCKClock.encodeVector(updatedParseVector, for: parseClock) else { + guard let updatedClock = PCKClock.encodeVector(updatedParseVector, for: parseClock) else { await self.remoteStatus.updateKnowledgeVector(parseVector) // revert await self.remoteStatus.notSynchronzing() completion(ParseCareKitError.couldntUnwrapClock)