我有wishlistVC,它具有如下图所示的集合视图:
我有这样的产品领域模型:
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>()
}
和WishList领域模型如下:
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
上,它将影响领域数据库中的Product.self,它将继续在“产品领域数据库”中添加产品,如您所见。下图所示,“产品”中有9个数据,但是您可以看到其中一些产品具有相同的productID,因此有3个产品的productID带有“ a”。
那么当我通过将产品附加到wishlist.products来修改WishList
时,如何避免在“产品领域数据库”(Product.self)中添加具有相同productID的产品?
我还尝试使用以下方法添加主键:
override static func primaryKey() -> String? {
return "productID"
}
但是它使崩溃并显示消息:
***由于未捕获的异常“ RLMException”而终止应用程序,原因:“尝试使用现有对象创建类型为“产品”的对象 主键值“ a”。
由于我添加了productID ='a',因此会引发错误
我该怎么办?如何将产品附加到WishList模型,但是我也可以避免将具有相同productID的同一产品添加到Product Realm数据库模型(Product.self)?
我使用错误的方法将产品添加到愿望清单吗?