从第二个recordType加载字段并附加到主数组

时间:2017-01-01 13:36:36

标签: ios swift cloudkit

我在CloudKit容器中有两个recordTypes。一个包含礼品券的详细信息,另一个包含它们的售卖包。

表2是孩子。

表1(giftcert):

  • id(int)
  • giftto(string)
  • giftfrom(string)
  • 包(参考)

表2(包):

  • pack_id(int)
  • pack_name(string)
  • pack_amount(int)

到目前为止,我有以下代码 - 如果这是一个SQL语句,它看起来像:

SELECT * FROM giftcert LEFT JOIN package ON package = pack_id WHERE 1 SORT BY id ASC

我想拥有来自" giftcert"的所有记录。在TableView中显示(这已经有了)。从那里,我希望能够在同一个单元格上显示相应的pack_name。

所以在单元格中,您将拥有:

  • 标签1 = giftcert.id
  • 标签2 = giftcert.giftto
  • 标签3 = package.pack_name

giftcert.swift

struct giftcert {

    let record: CKRecord!
    let id: Int
    let giftto: String
    let giftfrom: String
    let status: Int
    let expire: Date

    init(id: Int, giftto: String, giftfrom: String, status: Int, expire: Date) {
        self.id = id
        self.giftto = giftto
        self.giftfrom = giftfrom
        self.status = status
        self.expire = expire
        record = CKRecord(recordType: "giftcert")
        record["id"] = id as CKRecordValue?
        record["giftto"] = giftto as CKRecordValue?
        record["giftfrom"] = giftfrom as CKRecordValue?
        record["status"] = status as CKRecordValue?
        record["expire"] = expire as CKRecordValue?
    }

    init(record : CKRecord) {
        self.record = record
        self.id = record["id"] as! Int
        self.giftto = record["giftto"] as! String
        self.giftfrom = record["giftfrom"] as! String
        self.status = record["status"] as! Int
        self.expire = record["expire"] as! Date

    }
}

Model.swift

func refreshPublicDB() {
        let predicate = NSPredicate(value: true)
        let query = CKQuery(recordType: "giftcert", predicate: predicate)
        let sort = NSSortDescriptor(key: "id", ascending: true)
        query.sortDescriptors = [sort]

        publicDB.perform(query, inZoneWith: nil) { results, error in
            if let error = error {
                DispatchQueue.main.async {
                    self.delegate?.errorUpdating(error as NSError)
                }
            } else {
                self.items.removeAll(keepingCapacity: true)
                for record in results! {
                    let item = giftcert(record: record)
                    self.items.append(item)
                }

                DispatchQueue.main.async {
                    self.delegate?.publicDBUpdated()
                }
            }
        }
    }

0 个答案:

没有答案