Skip to content

Commit b5d013f

Browse files
committed
Encode vector as string for RevisionRecord
1 parent 5a74b6b commit b5d013f

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

Sources/ParseCareKit/Models/PCKClock.swift

+20-9
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ public struct PCKClock: ParseObject {
4646
return updated
4747
}
4848

49-
func decodeClock() throws -> OCKRevisionRecord.KnowledgeVector {
50-
guard let data = self.vector?.data(using: .utf8) else {
49+
static func decodeVector(_ clock: Self) throws -> OCKRevisionRecord.KnowledgeVector {
50+
try decodeVector(clock.vector)
51+
}
52+
53+
static func decodeVector(_ vector: String?) throws -> OCKRevisionRecord.KnowledgeVector {
54+
guard let data = vector?.data(using: .utf8) else {
5155
let errorString = "Could not get data as utf8"
5256
if #available(iOS 14.0, watchOS 7.0, *) {
5357
Logger.clock.error("\(errorString)")
@@ -74,15 +78,22 @@ public struct PCKClock: ParseObject {
7478
}
7579
}
7680

77-
func encodeClock(_ clock: OCKRevisionRecord.KnowledgeVector) -> Self? {
81+
static func encodeVector(_ vector: OCKRevisionRecord.KnowledgeVector, for clock: Self) -> Self? {
82+
guard let cloudVectorString = encodeVector(vector) else {
83+
return nil
84+
}
85+
var mutableClock = clock
86+
mutableClock.vector = cloudVectorString
87+
return mutableClock
88+
}
89+
90+
static func encodeVector(_ vector: OCKRevisionRecord.KnowledgeVector) -> String? {
7891
do {
79-
let json = try JSONEncoder().encode(clock)
92+
let json = try JSONEncoder().encode(vector)
8093
guard let cloudVectorString = String(data: json, encoding: .utf8) else {
8194
return nil
8295
}
83-
var mutableClock = self
84-
mutableClock.vector = cloudVectorString
85-
return mutableClock
96+
return cloudVectorString
8697
} catch {
8798
if #available(iOS 14.0, watchOS 7.0, *) {
8899
Logger.clock.error("Clock.encodeClock(): \(error.localizedDescription, privacy: .private).")
@@ -156,7 +167,7 @@ public struct PCKClock: ParseObject {
156167
switch result {
157168

158169
case .success(let foundVector):
159-
completion(foundVector, try? foundVector.decodeClock(), nil)
170+
completion(foundVector, try? decodeVector(foundVector), nil)
160171
case .failure(let error):
161172
if !createNewIfNeeded {
162173
completion(nil, nil, error)
@@ -169,7 +180,7 @@ public struct PCKClock: ParseObject {
169180
updatedVector.create(callbackQueue: ParseRemote.queue) { result in
170181
switch result {
171182
case .success(let savedVector):
172-
completion(savedVector, try? updatedVector.decodeClock(), nil)
183+
completion(savedVector, try? decodeVector(updatedVector), nil)
173184
case .failure(let error):
174185
completion(nil, nil, error)
175186
}

Sources/ParseCareKit/Models/PCKRevisionRecord.swift

+17-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,20 @@ struct PCKRevisionRecord: ParseObject, Equatable, Codable {
4242

4343
/// A knowledge vector indicating the last known state of each other device
4444
/// by the device that authored this revision record.
45-
var knowledgeVector: OCKRevisionRecord.KnowledgeVector?
45+
var knowledgeVector: OCKRevisionRecord.KnowledgeVector? {
46+
get {
47+
try? PCKClock.decodeVector(vector)
48+
}
49+
set {
50+
guard let newValue = newValue else {
51+
vector = nil
52+
return
53+
}
54+
vector = PCKClock.encodeVector(newValue)
55+
}
56+
}
57+
58+
public var vector: String?
4659

4760
var objects: [any PCKVersionable] {
4861
guard let entities = entities else {
@@ -95,7 +108,7 @@ struct PCKRevisionRecord: ParseObject, Equatable, Codable {
95108

96109
enum CodingKeys: String, CodingKey {
97110
case objectId, createdAt, updatedAt, className,
98-
ACL, knowledgeVector, entities,
111+
ACL, vector, entities,
99112
logicalClock, clock, clockUUID
100113
}
101114

@@ -203,8 +216,7 @@ extension PCKRevisionRecord {
203216
self.createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt)
204217
self.updatedAt = try container.decodeIfPresent(Date.self, forKey: .updatedAt)
205218
self.ACL = try container.decodeIfPresent(ParseACL.self, forKey: .ACL)
206-
self.knowledgeVector = try container.decodeIfPresent(OCKRevisionRecord.KnowledgeVector.self,
207-
forKey: .knowledgeVector)
219+
self.vector = try container.decodeIfPresent(String.self, forKey: .vector)
208220
self.entities = try container.decodeIfPresent([PCKEntity].self, forKey: .entities)
209221
self.clock = try container.decodeIfPresent(PCKClock.self, forKey: .clock)
210222
self.logicalClock = try container.decodeIfPresent(Int.self, forKey: .logicalClock)
@@ -218,7 +230,7 @@ extension PCKRevisionRecord {
218230
try container.encodeIfPresent(createdAt, forKey: .createdAt)
219231
try container.encodeIfPresent(updatedAt, forKey: .updatedAt)
220232
try container.encodeIfPresent(ACL, forKey: .ACL)
221-
try container.encodeIfPresent(knowledgeVector, forKey: .knowledgeVector)
233+
try container.encodeIfPresent(vector, forKey: .vector)
222234
try container.encodeIfPresent(entities, forKey: .entities)
223235
try container.encodeIfPresent(clock, forKey: .clock)
224236
try container.encodeIfPresent(logicalClock, forKey: .logicalClock)

Sources/ParseCareKit/ParseRemote.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public class ParseRemote: OCKRemoteSynchronizable {
522522
}
523523
cloudVector.merge(with: localClock)
524524

525-
guard let updatedClock = parseClock.encodeClock(cloudVector) else {
525+
guard let updatedClock = PCKClock.encodeVector(cloudVector, for: parseClock) else {
526526
await self.remoteStatus.setNotSynchronzing()
527527
completion(ParseCareKitError.couldntUnwrapClock)
528528
return

Tests/ParseCareKitTests/EncodingCareKitTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ class ParseCareKitTests: XCTestCase {
12661266
let entity = OCKEntity.patient(careKit)
12671267
let remoteUUID = UUID()
12681268
let clock = PCKClock(uuid: remoteUUID)
1269-
let clockVector = try clock.decodeClock()
1269+
let clockVector = try PCKClock.decodeVector(clock)
12701270
let logicalClockValue = clockVector.clock(for: remoteUUID)
12711271
let careKitRecord = OCKRevisionRecord(entities: [entity], knowledgeVector: clockVector)
12721272

@@ -1295,7 +1295,7 @@ class ParseCareKitTests: XCTestCase {
12951295
}
12961296

12971297
// Test PCKRevisionRecord encoding/decoding to Parse Server
1298-
let encoded = try ParseCoding.jsonEncoder().encode(parseRecord)
1298+
let encoded = try ParseCoding.parseEncoder().encode(parseRecord)
12991299
let decoded = try ParseCoding.jsonDecoder().decode(PCKRevisionRecord.self, from: encoded)
13001300
guard let decodedEntities = decoded.entities else {
13011301
XCTFail("Should have unwrapped")

0 commit comments

Comments
 (0)