Skip to content

Commit 9463925

Browse files
committed
revert back to subscribing to clock for sync
1 parent fa85b46 commit 9463925

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

Sources/ParseCareKit/Models/RemoteSynchronizing.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ actor RemoteSynchronizing {
2828
self.clock = clock
2929
}
3030

31-
func hasNewerRevision(_ logicalClock: Int, for uuid: UUID) -> Bool {
31+
func hasNewerRevision(_ vector: OCKRevisionRecord.KnowledgeVector, for uuid: UUID) -> Bool {
3232
guard !isSynchronizing else {
3333
return false
3434
}
3535
guard let currentClock = knowledgeVector?.clock(for: uuid) else {
3636
return true
3737
}
38-
return logicalClock > currentClock
38+
return vector.clock(for: uuid) > currentClock
3939
}
4040
}

Sources/ParseCareKit/ParseCareKitLog.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ extension Logger {
5252
static let pushRevisions = Logger(subsystem: subsystem, category: "\(category).pushRevisions")
5353
static let syncProgress = Logger(subsystem: subsystem, category: "\(category).syncProgress")
5454
static let clock = Logger(subsystem: subsystem, category: "\(category).clock")
55-
static let revisionRecordSubscription = Logger(subsystem: subsystem,
56-
category: "\(category).revisionRecordSubscription")
55+
static let clockSubscription = Logger(subsystem: subsystem, category: "\(category).clockSubscription")
5756
static let defaultACL = Logger(subsystem: subsystem, category: "\(category).defaultACL")
5857
static let initializer = Logger(subsystem: subsystem, category: "\(category).initializer")
5958
static let deinitializer = Logger(subsystem: subsystem, category: "\(category).deinitializer")

Sources/ParseCareKit/ParseRemote.swift

+25-24
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public class ParseRemote: OCKRemoteSynchronizable {
4141
public var pckStoreClassesToSynchronize: [PCKStoreClass: any PCKVersionable]!
4242

4343
private weak var parseDelegate: ParseRemoteDelegate?
44-
private var revisionRecordSubscription: SubscriptionCallback<PCKRevisionRecord>?
44+
private var clockRecordSubscription: SubscriptionCallback<PCKClock>?
4545
private var subscribeToServerUpdates: Bool
4646
private let remoteStatus = RemoteSynchronizing()
47-
private let revisionRecordQuery: Query<PCKRevisionRecord>
47+
private let clockQuery: Query<PCKClock>
4848

4949
/**
5050
Creates an instance of ParseRemote.
@@ -67,7 +67,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
6767
self.pckStoreClassesToSynchronize = try PCKStoreClass.patient.getConcrete()
6868
self.customClassesToSynchronize = nil
6969
self.uuid = uuid
70-
self.revisionRecordQuery = PCKRevisionRecord.query(RevisionRecordKey.clockUUID == uuid)
70+
self.clockQuery = PCKClock.query(ClockKey.uuid == uuid)
7171
self.automaticallySynchronizes = auto
7272
self.subscribeToServerUpdates = subscribeToServerUpdates
7373
if let currentUser = try? await PCKUser.current() {
@@ -144,7 +144,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
144144
deinit {
145145
Task {
146146
do {
147-
try await revisionRecordQuery.unsubscribe()
147+
try await clockQuery.unsubscribe()
148148
Logger.deinitializer.error("Unsubscribed from clock query")
149149
} catch {
150150
Logger.deinitializer.error("Couldn't unsubscribe from clock query")
@@ -189,36 +189,37 @@ public class ParseRemote: OCKRemoteSynchronizable {
189189
do {
190190
_ = try await PCKUser.current()
191191
guard self.subscribeToServerUpdates,
192-
self.revisionRecordSubscription == nil else {
192+
self.clockRecordSubscription == nil else {
193193
return
194194
}
195195

196196
do {
197-
self.revisionRecordSubscription = try await self.revisionRecordQuery.subscribeCallback()
198-
self.revisionRecordSubscription?.handleEvent { (_, event) in
197+
self.clockRecordSubscription = try await self.clockQuery.subscribeCallback()
198+
self.clockRecordSubscription?.handleEvent { (_, event) in
199199
switch event {
200-
case .created(let updatedRevision), .updated(let updatedRevision), .entered(let updatedRevision):
201-
guard let logicalClock = updatedRevision.logicalClock else {
202-
Logger
203-
.revisionRecordSubscription
204-
.error("RevisionRecord missing required \"logicalClock\" key: \(updatedRevision)")
205-
return
206-
}
207-
Task {
208-
guard await self.remoteStatus.hasNewerRevision(logicalClock, for: self.uuid) else {
209-
return
200+
case .created(let updatedClock), .updated(let updatedClock), .entered(let updatedClock):
201+
do {
202+
let updatedVector = try PCKClock.decodeVector(updatedClock)
203+
Task {
204+
guard await self.remoteStatus.hasNewerRevision(updatedVector, for: self.uuid) else {
205+
return
206+
}
207+
self.parseDelegate?.didRequestSynchronization(self)
208+
Logger
209+
.clockSubscription
210+
.log("Parse subscription is notifying that there are updates on the server")
210211
}
211-
self.parseDelegate?.didRequestSynchronization(self)
212+
} catch {
212213
Logger
213-
.revisionRecordSubscription
214-
.log("Parse remote has updates available")
214+
.clockSubscription
215+
.error("Could not decode server clock: \(error)")
215216
}
216217
default:
217218
return
218219
}
219220
}
220221
} catch {
221-
Logger.revisionRecordSubscription.error("Couldn't subscribe to RevisionRecord query")
222+
Logger.clockSubscription.error("Couldn't subscribe to RevisionRecord query")
222223
return
223224
}
224225
} catch {
@@ -330,8 +331,8 @@ public class ParseRemote: OCKRemoteSynchronizable {
330331
completion(ParseCareKitError.requiredValueCantBeUnwrapped)
331332
return
332333
}
333-
let logicalClock = parseVector.clock(for: self.uuid)
334-
guard await !self.remoteStatus.hasNewerRevision(logicalClock, for: self.uuid) else {
334+
335+
guard await !self.remoteStatus.hasNewerRevision(parseVector, for: self.uuid) else {
335336
let errorString = "New knowledge on server. Pull first then try again"
336337
Logger.pushRevisions.error("\(errorString)")
337338
await self.remoteStatus.notSynchronzing()
@@ -351,7 +352,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
351352
// 8. Push conflict resolutions + local changes to remote
352353
self.notifyRevisionProgress(0,
353354
total: deviceRevisions.count)
354-
355+
let logicalClock = parseVector.clock(for: self.uuid)
355356
for (index, deviceRevision) in deviceRevisions.enumerated() {
356357
do {
357358
let remoteRevision = try PCKRevisionRecord(record: deviceRevision,

0 commit comments

Comments
 (0)