在领域

时间:2017-04-28 11:58:06

标签: ios swift realm one-to-many

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

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

ItemList对象:

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

项目对象:

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

主控制器:

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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在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是唯一的。

相关问题