Like the Title says, I would like to use Cloudkit with the Apple Watch to fetch information and list the names of items I have stored in the private field. However, whenever I run the code I just get a blank screen and I am not sure why. When I change the identifier for the container it throws an error that it cannot make a connection leading me to think there is some connection going on but that it is getting hung somewhere, here is my code for the project:
Most of this code is followed from a guide that I found, but it does not seem to work on the Apple Watch and I am not sure why as there seem to be barely any guides or documentation on this.
struct CloudKitHelper {
// MARK: - record types
struct RecordType {
static let Plants = "CD_Plant"
}
// MARK: - errors
enum CloudKitHelperError: Error {
case recordFailure
case recordIDFailure
case castFailure
case cursorFailure
}
static func getRecords(completion: @escaping (Result<ListElement, Error>) -> ()) {
let pred = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "CD_identifier", ascending: false)
let query = CKQuery(recordType: RecordType.Plants, predicate: pred)
query.sortDescriptors = [sort]
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["CD_identifier"]
operation.resultsLimit = 50
operation.recordFetchedBlock = { record in
DispatchQueue.main.async {
guard let recordID = record["CD_identifier"] as? String else { return }
guard let name = record["CD_name"] as? String else { return }
guard let lastWaterDate = record["CD_lastWateringDate"] as? Date else { return }
guard let lastFertDate = record["CD_lastFertDate"] as? Date else { return }
guard let watering = record["CD_watering"] as? Int64 else { return }
guard let fertilizing = record["CD_fertilize"] as? Int64 else { return }
let listElement = ListElement(recordID: recordID, name: name, lastWaterDate: lastWaterDate, lastFertDate: lastFertDate, watering: watering, fertilizing: fertilizing)
completion(.success(listElement))
}
}
operation.queryCompletionBlock = { (/*cursor*/ _, err) in
DispatchQueue.main.async {
if let err = err {
completion(.failure(err))
return
}
// guard let cursor = cursor else {
// completion(.failure(CloudKitHelperError.cursorFailure))
// return
// }
// print("Cursor: (String(describing: cursor))")
}
}
CKContainer(identifier: "iCloud.com.base.identifier").privateCloudDatabase.add(operation)
}
}
Here is the content view that I am using:
'''
import SwiftUI
struct ContentView: View {
@EnvironmentObject var listElements: ListElements
@State private var newItem = ListElement(name: "", lastWaterDate: Date.distantPast, lastFertDate: Date.distantPast, watering: 0, fertilizing: 0)
@State private var editedItem = ListElement(name: "", lastWaterDate: Date.distantPast, lastFertDate: Date.distantPast, watering: 0, fertilizing: 0)
var body: some View {
NavigationView{
VStack{
List(listElements.items) { item in
HStack{
Text("Plant Name: (item.name)")
}
}
}
.onAppear {
// MARK: - Fetch From Cloudkit
CloudKitHelper.getRecords { (result) in
switch result {
case .success(let newItem):
self.listElements.items.append(newItem)
print("Successfully Fetched Item.")
case .failure(let err):
print(err.localizedDescription)
}
}
}
}
}
}
'''
question from:
https://stackoverflow.com/questions/65945674/using-cloudkit-with-apple-watch 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…