OStack程序员社区-中国程序员成长平台

标题: ios - 如何合并来自多个 CloudKit 表的数据? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:22
标题: ios - 如何合并来自多个 CloudKit 表的数据?

我正在寻找一种将数据从多个 CloudKit 表加载到一个对象中的方法。 现在我正在从一个 CloudKit 表(recordType)加载数据,但是如何在下面的函数中组合这些数据?我需要添加连接到 Clients 表的事件表。所以一个客户可以有多个事件。

我的客户模型:

class Client: NSObject {
  var recordID: CKRecordID!
  var name: String?
  var activities: [Activity]?
  var image: UIImage?
  var email: String?
  var phone: String?
  var rate: Int?
}

我的事件对象:

class Activity: NSData {
  var name: String?
  var date: Date?
}

仅加载客户端的功能:

private func loadClients() {
    let pred = NSPredicate(value: true)
    let sort = NSSortDescriptor(key: "Name", ascending: true)
    let query = CKQuery(recordType: "Clients", predicate: pred)
    query.sortDescriptors = [sort]

    let operation = CKQueryOperation(query: query)

    var newClients = [Client]()

    operation.recordFetchedBlock = { record in
      let client = Client()
      client.recordID = record.recordID
      client.name = record["Name"] as? String
      client.email = record["Email"] as? String
      client.phone = record["hone"] as? String
      client.rate = record["Rate"] as? Int

      newClients.append(client)
    }

    operation.queryCompletionBlock = { [unowned self] (cursor, error) in
      DispatchQueue.main.async {
        if error == nil {
          ViewController.isDirty = false
          self.clients = newClients

          if self.clients.count == 0 {
            self.messageLabel.text = "No values in the database"
          } else {
            self.tableView.reloadData()
          }

          self.updateView()
        } else {
          self.messageLabel.text = "Error loading data"
          print("ERROR: \(error!.localizedDescription)")
          self.updateView()
        }
      }
    }

    CKContainer.default().privateCloudDatabase.add(operation)
 }



Best Answer-推荐答案


据我了解,您想使用一个请求将 ClientActivity 数组一起加载。

我认为使用 CloudKit 是不可能的。 但是你可以在收到Client记录后加载Activity:

    if let activitiesIDs = clientRecord["activities"] as? [CKRecordID] {
        let operation = CKFetchRecordsOperation(recordIDs: activitiesIDs)
        operation.fetchRecordsCompletionBlock = { (records, error) in

        }

        let database = CKContainer.default().publicCloudDatabase
        database.add(operation)
    }

关于ios - 如何合并来自多个 CloudKit 表的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249475/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4