@@ -160,56 +160,49 @@ public struct PCKClock: ParseObject {
160
160
return try await newClock. create ( )
161
161
}
162
162
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
+ }
207
199
}
208
200
}
209
201
210
202
extension PCKClock {
211
203
init ( uuid: UUID ) {
212
204
self . uuid = uuid
205
+ self . objectId = uuid. uuidString
213
206
knowledgeVectorString = " { \" processes \" :[{ \" id \" : \" \( uuid) \" , \" clock \" :0}]} "
214
207
ACL = PCKUtility . getDefaultACL ( )
215
208
}
0 commit comments