快速更新领域数据库中的值

时间:2018-09-12 13:52:36

标签: ios swift realm

我正在尝试更新领域数据库中的值。如果用户选择包含值的行,我希望能够更新该行的值。这是我的代码,但没有更新,而是在数据库中创建了另一个值

func updateTodoList(todoList: TodoListModel, name: String, description: String, createdDate: Date, remiderDate: Date, photo: Data, isCompleted: Bool) -> Void {

        try! database.write {

            if name != "" {
                todoList.name = name
            } else {
                todoList.name = "No extra information"
            }

            todoList.desc = description
            todoList.createdDate = createdDate
            todoList.remiderDate = remiderDate
            todoList.photo = photo
            todoList.isCompleted = false

        }

    }

我确实选择了行

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let todoList = todoItems?[indexPath.row]

        let storyBoard: UIStoryboard = UIStoryboard(name: "AddTodoListSB", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: Constants.ADD_TODO_SB) as! AddTodoListVC
        newViewController.loadViewIfNeeded()

        let min = Date()
        let max = Date().addingTimeInterval(60 * 60 * 60 * 60)
        guard let itemPhoto = UIImagePNGRepresentation(newViewController.imageView.image!) else {return}
        newViewController.picker.minimumDate = min
        newViewController.picker.maximumDate = max
//        newViewController.showDateTimePicker(sender: <#T##AnyObject#>)
        newViewController.picker.completionHandler = { date in
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
            self.title = formatter.string(from: date)

            let reminder = formatter.string(from: date)

            TodoListFunctions.instance.updateTodoList(todoList: todoList!, name: newViewController.titleTxtField.text!, description: newViewController.moreInfoTxtView.text!, createdDate: (todoList?.createdDate)!, remiderDate: formatter.date(from: reminder)!, photo: itemPhoto, isCompleted: false)


        }

        tableView.reloadData()
        self.present(newViewController, animated: true, completion: nil)

    }

// TodolistModel

class TodoListModel: Object {

    @objc dynamic var id = UUID().uuidString
    @objc dynamic var name: String = ""
    @objc dynamic var desc: String = "No Description"
    @objc dynamic var photo: Data? = nil
    @objc dynamic var createdDate: Date?
    @objc dynamic var remiderDate: Date?
    @objc dynamic var isCompleted = false


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

    let parentCategory = LinkingObjects(fromType: CategoryModel.self, property: "items")

}

更多代码将应要求提供

1 个答案:

答案 0 :(得分:2)

要更新对象,它必须具有主键,并且在编辑后使用

var nodes = [];
for (var i = 0; i < elements.length; i++)
    nodes.push({ group: "nodes", data: { id: i });
}
var k = 0;
for (var i = 0; i < elements.length; i+=2)
    nodes.push({ group: "edges", data: { id: "e" + k, source: "n" + i, target: "n" + (i + 1) } });
    k += 1;
}
cy.add(nodes);

//

 // if it doesn't exist it'll be added
 database.add(editedObjc, update: true)