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

        venue.id = record[“id”]

        venue.name = record[“name”]

        venue.address = record[“address”]

        newVenues.append(venue)

    }

  

    operation.queryCompletionBlock = {(cursor, error) in

        DispatchQueue.main.async {

            if error == nil {

                data = newVenues

            } else {

                print(error!.localizedDescription)

            }

        }

    }

  

    CKContainer.default().publicCloudDatabase.add(operation)

  

    return data

}

“`

 

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

Powered by WPeMatico

About

You may also like...

Comments are closed.