【问题标题】:How to avoid adding the same data model that has the same primary key in realm database?如何避免在领域数据库中添加具有相同主键的相同数据模型?
【发布时间】:2018-11-29 05:19:06
【问题描述】:

我在两个模型之间有一对多的关系,ProductWishList 就像下面的代码

class Product : Object {

    @objc dynamic var productID : String = ""
    @objc dynamic var name : String = ""
    @objc dynamic var unitPrice: Double = 0.0
    @objc dynamic var imagePath : String = ""
    @objc dynamic var quantity = 0
    @objc dynamic var hasBeenAddedToWishList : Bool = false
    var parentCategory = LinkingObjects(fromType: WishList.self, property: "products")

    convenience init(productID : String, name: String, unitPrice: Double, imagePath: String, quantity: Int = 1, hasBeenAddedToWishList: Bool = false) {
        self.init()

        self.productID = productID
        self.name = name
        self.unitPrice = unitPrice
        self.imagePath = imagePath
        self.quantity = quantity
        self.hasBeenAddedToWishList = hasBeenAddedToWishList
    }

    override static func primaryKey() -> String? {
        return "productID"
    }

}

和愿望清单:

class WishList : Object {
    @objc dynamic var userID: String = ""
    var products = List<Product>()
}

当按下上图中的爱心按钮时,我尝试使用下面的代码向愿望清单添加或删除产品:

    // 1. get the wishlist based on UserID

    let allWishList = realm.objects(WishList.self)
    let theWishList = allWishList.filter("userID CONTAINS[cd] %@", userID).first

    guard let userWishList = theWishList else {return}


    // 2. modify Wishlist data in Realm.

    if loveIconHasBeenFilled {

       guard let index = userWishList.products.index(where: {$0.productID == selectedProduct.productID}) else {return}

       do {
        // remove data from realm database            

          try realm.write {
            userWishList.products.remove(at: index)
          }

        } catch {
          // error Handling
        }



    } else {

      do {

         // add product to wishlist model in realm database

         try realm.write {
            userWishList.products.append(selectedProduct)
         }

      } catch {
        // error Handling
      }


   }

这是Realm Browser中的数据

问题是……

当我第一次运行应用程序时,我可以添加,然后删除,然后再次将产品添加到愿望清单,领域数据库中的产品数量仍然相同(都有唯一的产品ID)

但是当我重新启动应用程序并尝试单击那个爱按钮以再次将产品添加到愿望清单时,它会引发错误

'RLMException',原因:'试图创建一个类型的对象 具有现有主键值“a”的“产品”

这个错误是因为userWishList.products.append(selectedProduct)这行代码触发的,在将产品添加到WishList时,会在realm数据库中自动添加Product。因此,因为我不断添加具有相同 productID(主键)的相同产品,它会抛出该错误。

所以,我的问题是,如果 Product 具有相同的 productID(主键),如何避免添加,如果我可以在将产品添加到愿望清单时仅更新领域数据库中的产品,使用此功能会更好代码行:userWishList.products.append(selectedProduct)

【问题讨论】:

  • 也许你可以考虑创建另一个领域对象来保存 Product 和 WishList 之间的关系

标签: ios swift realm


【解决方案1】:

您可以检查所选产品的属性hasBeenAddedToWishList,如果属性为false,则仅添加它。

 if loveIconHasBeenFilled {
   //your logic to remove already added products

} else if !selectedProduct.hasBeenAddedToWishList { //<--- check if the product already exists in wishlist if not you add it

  do {

     // add product to wishlist model in realm database

     try realm.write {
        userWishList.products.append(selectedProduct)
     }

  } catch {
    // error Handling
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2018-01-17
    • 2021-04-22
    • 2017-10-11
    • 2014-03-11
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多