Hello everyone! I am pretty new to SwiftUI and have been trying to figure out a solution to the issue below for about a week.
I can successfully save values in CloudKit, and can load them within the operation.recordFetchedBlock
block using print(String(ping.value))
on line 15. However, I can not figure out a way to actually load them on the display within ContentView: View
For some reason I continue to get the following (empty array) in the output when using print(loadPing())
on line 9:
[]
No Error Found
Below is the code:
struct ContentView: View {
@StateObject var progress = UserProgress()
var body: some View{
VStack{
Button("Submit") {
progress.ping += 1
sendItem(counter: progress.ping)
print(loadPing())
}
Text("Ping Count \(progress.ping)")
}
}
}
class Ping: NSObject {
let id = UUID()
var recordID: CKRecord.ID!
var name: String!
var value: String!
}
loadPing() -> [Ping]{
var newPing = [Ping]()
let ping = Ping()
let query = CKQuery(recordType: "PingTest", predicate: NSPredicate(value: true))
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["value", "name"]
operation.recordFetchedBlock = { record in
ping.recordID = record.recordID
ping.name = record["name"]
ping.value = record["value"]
newPing.append(ping)
// print(String(ping.value))
}
operation.queryCompletionBlock = {(cursor, error) in
DispatchQueue.main.async {
if error == nil {
print("No Error Found")
} else {
print("Error was Found")
print(error!.localizedDescription)
}
}
}
CKContainer.default().publicCloudDatabase.add(operation)
return newPing
}
I would be very grateful if someone could help me figure out a way to populate the actual values from CloudKit onto my display in the ContentView
. I have sifted through various tutorials but nothing has worked so far.
You could use the new public sample projects to get started. Take a look at cloudkit-sample-privatedb for a simple starting point.