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.
Post
Replies
Boosts
Views
Activity
Hello everyone!
Im new to SwiftUI and I have been trying to pinpoint the best approach when sending and receiving data using CloudKit.
Is Core Data absolutely necessary to use when building an app with SwiftUI and CloudKit? I've read that Core Data is falling off, which is why I'm questionable on spending time learning it.
Couldn't I simply use SwiftUI and CloudKit without the use of anything else?
I do know that Core Data is NOT necessary when using CloudKit while writing Objective C, but I am wanting to stick solely with Swift as I have been told that Obj C is falling off too.
Thanks for the help!
Are Xcode 12's App and Game templates synonomous?
Would I be able to use the App template to create an impressive iPhone game? Is taking this path recommended?
Are there features (graphical, framework, functionality, etc) the Game template has that the App template does not?
What question(s) could I ask myself when trying to pick one template over the other?