How to retrieve data from CloudKit using SwiftUI?

I am using SwiftUI to make an app and storing the data on ICloud. Because all the code I can find relates to Swift and viewDidLoad and TableView, however this do not apply. I have written code and seems to retrieve it but will not return it to the ObservableObject to be able to display in the SwiftUI



The ObservableObject file:-


import SwiftUI

import Combine



final class ObservedData: ObservableObject {

    @Published var venues = venuesData





The query to retrieve data



import SwiftUI

import CloudKit



var venuesData: [Venue] = loadVenues()



func loadVenues() -> [Venue] {


    var data = [Venue]()


    let pred = NSPredicate(value: true)

    let sort = NSSortDescriptor(key: “id”, ascending: true)

    let query = CKQuery(recordType: “DeptfordHopData”, predicate: pred)

    query.sortDescriptors = [sort]


    let operation = CKQueryOperation(query: query)

    operation.desiredKeys = [“id”,”name”, “address”]

    operation.resultsLimit = 50


    var newVenues = [Venue]()


    operation.recordFetchedBlock = { record in

        let venue = Venue()

        venue.racordID = record.recordID = record[“id”] = record[“name”]

        venue.address = record[“address”]




    operation.queryCompletionBlock = {(cursor, error) in

        DispatchQueue.main.async {

            if error == nil {

                data = newVenues

            } else {








    return data




I have got data showing when do break in data but does not pass to venueData

Powered by WPeMatico

You may also like...

Comments are closed.