【问题标题】:Realm List not saving data领域列表不保存数据
【发布时间】:2020-05-24 01:15:38
【问题描述】:

我正在从 FireBase 获取数据并将其保存在我的领域中,但它没有按预期工作:

for doc in docs {
    let shopData = doc.data()
    let newShop = RLMShop()
    newShop.shopName = shopData["name"] as? String ?? "Empty Name"
    self.saveShop(shop: newShop) // My issue is here 
}

我的存档功能:

    func saveShop(shop: RLMShop) {
       do {
          try realm.write {
            realm.add(shop)
          }
       } catch {
        print("Error saving shop \(error)")
    }
}

调用保存函数并没有保存我的对象。

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

    您遇到的问题是您正在创建一个RLMShop 对象,但它没有链接到RLMShopsCategory 对象,因此您的shopsList 将不包含新对象。

    // Fetch the RLMShopsCategory that you wish to add the RLMShop too
    // Using Primary Key here just as an example
    let shopsCategory = realm.object(ofType: RLMShopsCategory.self, forPrimaryKey: "YourKey")
    
    for doc in docs {
        let shopData = doc.data()
        let newShop = RLMShop()
        newShop.shopName = // setting the properties etc
    
        // This is the part you are missing
        // You need to append the newShop to your shopsCategory object
        try! realm.write {
           shopsCategory.shopsList.append(newShop)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多