Skip to content

Commit 867995f

Browse files
authored
fix: Use Clock uuid as objectId (#202)
* fix: Use Clock uuid as objectId * add unit test
1 parent 39482b9 commit 867995f

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/cbaker6/CareKit.git",
15-
.upToNextMajor(from: "3.0.0-beta.42")),
15+
.upToNextMajor(from: "3.0.0-beta.45")),
1616
.package(url: "https://github.com/netreconlab/Parse-Swift.git",
1717
.upToNextMajor(from: "5.12.2"))
1818
],

ParseCareKit.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@
10561056
repositoryURL = "https://github.com/cbaker6/CareKit.git";
10571057
requirement = {
10581058
kind = upToNextMajorVersion;
1059-
minimumVersion = "3.0.0-beta.42";
1059+
minimumVersion = "3.0.0-beta.45";
10601060
};
10611061
};
10621062
/* End XCRemoteSwiftPackageReference section */

Sources/ParseCareKit/Models/PCKClock.swift

+37-44
Original file line numberDiff line numberDiff line change
@@ -160,56 +160,49 @@ public struct PCKClock: ParseObject {
160160
return try await newClock.create()
161161
}
162162

163-
static func fetchFromRemote(_ uuid: UUID,
164-
createNewIfNeeded: Bool,
165-
completion: @escaping(Result<Self, ParseError>) -> Void) {
166-
167-
// Fetch Clock from Remote
168-
let query = Self.query(ClockKey.uuid == uuid)
169-
query.first { result in
170-
171-
switch result {
172-
173-
case .success(let foundVector):
174-
completion(.success(foundVector))
175-
case .failure(let error):
176-
if !createNewIfNeeded {
177-
completion(.failure(error))
178-
} else {
179-
// This is the first time the Clock is user setup for this user
180-
Task {
181-
do {
182-
let newClock = try await new(uuid: uuid)
183-
completion(.success(newClock))
184-
} catch {
185-
guard let parseError = error as? ParseError else {
186-
let errorString = "Could not cast error to ParseError"
187-
Logger.clock.error("\(errorString): \(error)")
188-
completion(.failure(.init(message: errorString, swift: error)))
189-
return
190-
}
191-
Logger.clock.error("\(parseError)")
192-
completion(.failure(parseError))
193-
}
194-
}
195-
}
196-
}
197-
}
198-
}
199-
200-
static func fetchFromRemote(_ uuid: UUID,
201-
createNewIfNeeded: Bool = false) async throws -> Self {
202-
try await withCheckedThrowingContinuation { continuation in
203-
Self.fetchFromRemote(uuid,
204-
createNewIfNeeded: createNewIfNeeded,
205-
completion: continuation.resume)
206-
}
163+
static func fetchFromRemote(
164+
_ uuid: UUID,
165+
createNewIfNeeded: Bool = false
166+
) async throws -> Self {
167+
do {
168+
let fetchedClock = try await Self(uuid: uuid).fetch()
169+
return fetchedClock
170+
} catch let originalError {
171+
do {
172+
// Check if using an old version of ParseCareKit
173+
// which didn't have the uuid as the objectID.
174+
let query = Self.query(ClockKey.uuid == uuid)
175+
let queriedClock = try await query.first()
176+
return queriedClock
177+
} catch {
178+
guard createNewIfNeeded else {
179+
throw originalError
180+
}
181+
do {
182+
let newClock = try await new(uuid: uuid)
183+
return newClock
184+
} catch {
185+
guard let parseError = error as? ParseError else {
186+
let errorString = "Could not cast error to ParseError"
187+
Logger.clock.error("\(errorString): \(error)")
188+
let parseError = ParseError(
189+
message: errorString,
190+
swift: error
191+
)
192+
throw parseError
193+
}
194+
Logger.clock.error("\(parseError)")
195+
throw parseError
196+
}
197+
}
198+
}
207199
}
208200
}
209201

210202
extension PCKClock {
211203
init(uuid: UUID) {
212204
self.uuid = uuid
205+
self.objectId = uuid.uuidString
213206
knowledgeVectorString = "{\"processes\":[{\"id\":\"\(uuid)\",\"clock\":0}]}"
214207
ACL = PCKUtility.getDefaultACL()
215208
}

Tests/ParseCareKitTests/EncodingCareKitTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,13 @@ class ParseCareKitTests: XCTestCase {
13261326
XCTFail("Should have been patient")
13271327
}
13281328
}
1329+
1330+
func testClock() async throws {
1331+
let uuid = UUID()
1332+
let clock = PCKClock(uuid: uuid)
1333+
XCTAssertEqual(clock.uuid, uuid)
1334+
XCTAssertEqual(clock.objectId, uuid.uuidString)
1335+
}
13291336
/*
13301337
func testAddContact() async throws {
13311338
let contact = OCKContact(id: "test", givenName: "hello", familyName: "world", carePlanUUID: nil)

0 commit comments

Comments
 (0)