gpt4 book ai didi

ios - 在 Realm 中删除一对多关系的项目

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:19 25 4
gpt4 key购买 nike

以下代码适用于添加新的 ItemList 和向 ItemList 添加新的 Items。我遇到的问题是从列表中删除所有项目。换句话说,我有一个按钮 (deleteAllItems),它应该从所选列表中删除所有项目,但我现在拥有它的方式删除了 ALL Realm 中的项目,无论它们属于哪个列表。

创建多个 ItemList 的正确方法是什么,它包含多个 Item 并且能够从中删除所有 Item某个列表?

项目列表对象:

class ItemList: Object {
dynamic var listName = ""
dynamic var createdAt = NSDate()
let items = List<Item>()
}

项目对象:

class Item: Object {
dynamic var productName = ""
dynamic var createdAt = NSDate()
}

主 Controller :

class ViewController: UIViewController, UITableViewDataSource{

@IBOutlet weak var tableListOfItems: UITableView!
@IBOutlet weak var inputProductName: UITextField!
@IBOutlet weak var inputListName: UITextField!

var allItems : Results<Item>!

override func viewDidLoad() {
super.viewDidLoad()
updateData()
}

@IBAction func addNewList(_ sender: Any) {
let list = ItemList()
list.listName = inputListName.text!
try! realm.write {
realm.add(list)
}
}

@IBAction func addNewItem(_ sender: Any) {
let newItem = Item()
newItem.productName = inputProductName.text!

let list = realm.objects(ItemList.self).filter("listName = 'List Name Here'").first!

try! realm.write {
list.items.append(newItem)
updateData()
}
}

func updateData(){
allItems = realm.objects(Item.self)
tableListOfItems.reloadData()
}

/// This deletes every Item in Realm which is not what
/// I want. I want to delete only items that belong to
/// a certain list. I tried...
/// let list = realm.objects(ItemList.self).filter("listName = 'Default List'").first!
/// list.items.removeAll()
@IBAction func deleteAllItems(_ sender: Any) {
try! realm.write {
realm.delete(realm.objects(Item.self))
updateData()
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
let data = allItems[indexPath.row]
cell.textLabel?.text = data.productName
return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
if let item = allItems?[indexPath.row] {
try! realm.write {
realm.delete(item)
}
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
}
}

最佳答案

在您的 deleteAllItems 函数中,您应该过滤它们。如果不进行过滤,这是预期的行为,因为您只指定了一个对象类,所以 Realm 显然会删除所有匹配该类的对象。

如果您想从 Realm 中删除列表中包含的所有项目,但保留空列表,您需要按以下方式更改 deleteAllItems 函数:

@IBAction func deleteAllItems(_ sender: Any) {
guard let listToDelete = realm.objects(ItemList.self).filter("listName = %@",listNameToDelete).first else { return }
try! realm.write {
for item in listToDelete.items {
realm.delete(realm.objects(Item.self).filter("productName = %@", item.productName).first)
updateData()
}
}
}

listNameToDelete 应该在您的类中的函数外部声明,或者您也可以在函数内部声明它,具体取决于您实际想要实现的目标。此外,此实现假定您的 listNames 和 productNames 是唯一的。

关于ios - 在 Realm 中删除一对多关系的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43679687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com