代码之家  ›  专栏  ›  技术社区  ›  sarah

如何避免在多对多关系的领域数据库中添加相同的数据?

  •  0
  • sarah  · 技术社区  · 6 年前

    enter image description here

    我有这样的产品领域模型:

    class Product : Object {
    
        @objc dynamic var productID : String = ""
        @objc dynamic var name : String = ""
        @objc dynamic var unitPrice: Double = 0.0
        @objc dynamic var quantity = 0
        @objc dynamic var descriptionProduct : String = ""
        @objc dynamic var hasBeenAddedToWishList : Bool = false
        @objc dynamic var hasBeenAddedToCart : Bool = false
        @objc dynamic var isNewProduct : Bool = false
        var imagePaths = List<String>()
    }
    

    以及如下所示的愿望领域模型:

        class WishList : Object {
            @objc dynamic var userID: String = ""
            var products = List<Product>() // many to many relationship
    
       static func getWishListFromRealmDatabase() -> WishList {
    
            let userID = "1"
            let allWishList = RealmService.shared.realm.objects(WishList.self)
            let theWishList = allWishList.filter("userID CONTAINS[cd] %@", userID).first
    
    
            if let userWishList = theWishList {
                return userWishList
            } else {
                // WishList never setted up before in Realm database container
                // then create WishList in realm database
    
                let newWishList = WishList()
                newWishList.userID = userID
                newWishList.products = List<Product>()
                RealmService.shared.save(object: newWishList)
                return newWishList
            }
        }
    
    
        static func addProductToWishListRealmDatabase(userWishList: WishList, selectedProduct: Product) {
    
            // to check wheter the selected product from user is already in WishList or not
            if userWishList.products.filter("productID == %@", selectedProduct.productID).first == nil {
    
                RealmService.shared.save(expression: {
                    selectedProduct.hasBeenAddedToWishList = true
                    userWishList.products.append(selectedProduct)
                })
    
            }
    
    
        }
    
         }
    

    当用户点击“爱”按钮时,以下是用于将产品添加到愿望列表的代码:

    func addProductToWishListRealmDatabase(userWishList: WishList, selectedProduct: Product) {
    
            // to check wheter the selected product from user is already in WishList.products or not
            if userWishList.products.filter("productID == %@", selectedProduct.productID).first == nil {
    
                // write in realm database
                RealmService.shared.save(expression: { // <-- this is just a wrapper to avoid write do try catch block all over the place
    
                    selectedProduct.hasBeenAddedToWishList = true
                    userWishList.products.append(selectedProduct)
    
    
                })
    
            }
    
    
        }
    

    下面是我的WishListVC的完整简化代码:

    class WishListVC : UIViewController {
    
        @IBOutlet weak var wishListCollectionView: UICollectionView!
    
        private var userWishList : WishList?
        private var products = List<Product>()
        private var selectedProduct : Product?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            userWishList = WishList.getWishListFromRealmDatabase() // the definition is in the code above
            guard let userWishList = userWishList else {return}
            products = userWishList.products
    
        }
    
    
    
    }
    
    extension WishListVC : ListProductCellDelegate {
    
        func likeButtonDidTapped(at selectedIndexPath: IndexPath, productHasBeenLiked: Bool, collectionView: UICollectionView) {
    
            guard let userWishList = userWishList else {return}
            let selectedProduct = products[selectedIndexPath.item]
    
            if productHasBeenLiked {
                WishList.removeProductFromWishListRealmDatabase(userWishList: userWishList, selectedProduct: selectedProduct)
            } else {
                WishList.addProductToWishListRealmDatabase(userWishList: userWishList, selectedProduct: selectedProduct)
            }
    
            wishListCollectionView.reloadData()
            self.wishListCollectionView.isHidden = userWishList.products.isEmpty
    
        }
    
    }
    

    wishlist model

    enter image description here

    那么,当我修改时,如何避免在“产品领域数据库”(product.self)中添加具有相同productID的产品呢 WishList

    我还尝试使用以下方法添加主键:

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

    但它带来了信息崩溃:

    ***由于未捕获的异常“RLMException”而终止应用程序,原因:“正在尝试使用现有 主键值“a”

    它将抛出错误,因为我添加了productID='a'

    我该怎么办?如何将产品附加到WishList模型,但我也可以避免将具有相同productID的相同产品添加到产品领域数据库模型(product.self)?

    我是否使用了错误的方法将产品添加到愿望列表?

    0 回复  |  直到 6 年前