如何将主键添加到领域对象
how to add primary key to realm objects
我正在尝试让我的领域对象能够从应用程序进行编辑。这是我的 class:
import Foundation
import RealmSwift
class Category: Object {
@objc dynamic var name: String = ""
let items = List<Item>()
}
这就是我创建实例的方式 class:
let action = UIAlertAction(title: "Add", style: .default) { action in
let newCategory = Category()
newCategory.name = textField.text!
self.save(category: newCategory)
}
这是我的编辑方法:
func edit(category: Category) {
do {
try realm.write {
realm.add(category, update: .all)
}
}catch {
print("error editing category \(error)")
}
}
这就是我在按下编辑按钮时在 IBAction 中调用我的编辑方法的方式
let saveAction = UIAlertAction(title: "Save Changes", style: .default) { action in
let editedCategory = Category()
editedCategory.name = textField.text!
self.edit(category: editedCategory)
}
我得到的错误是:“由于未捕获的异常 'RLMException',正在终止应用程序,原因:''Category' 没有主键,无法更新'”
我已尝试在我的类别 class 中添加主键,但仍然无效。有帮助吗?
Realm 指南中涵盖了很多内容 Read and Write Data 但让我也提供一些细节。请注意,此答案反映了 Swift 10.10.0 之前的版本,并且某些内容在更高版本中发生了变化。
您尝试使用的方法
realm.add(category, update: .all)
称为 'upsert' 并且需要对象中的主键。
An upsert either inserts or updates an object depending on whether the
object already exists. Upserts require the data model to have a
primary key.
所以让我们重新工具化 Category 对象以包含主键 _id
class Category: Object {
@objc dynamic var _id: ObjectId = ObjectId.generate()
@objc dynamic var name: String = ""
let items = List<Item>()
override static func primaryKey() -> String? {
return "_id"
}
}
然后,当一个对象被创建时
let newCategory = Category()
newCategory.name = textField.text!
let realm = try! Realm()
try! realm.write {
realm.add(newCategory)
}
它将有一个主键
从那时起,您可以在写入中修改对象或通过它的主键更新插入它。
let someId = //the id of the newCategory created above
try! realm.write {
let anotherCat = Category(value: ["_id": someId, "name": "Frank"])
realm.add(anotherCat, update: .modified)
}
上面的代码将用新名称更新原来的 newCategory 对象,因为 _id 是相同的
我正在尝试让我的领域对象能够从应用程序进行编辑。这是我的 class:
import Foundation
import RealmSwift
class Category: Object {
@objc dynamic var name: String = ""
let items = List<Item>()
}
这就是我创建实例的方式 class:
let action = UIAlertAction(title: "Add", style: .default) { action in
let newCategory = Category()
newCategory.name = textField.text!
self.save(category: newCategory)
}
这是我的编辑方法:
func edit(category: Category) {
do {
try realm.write {
realm.add(category, update: .all)
}
}catch {
print("error editing category \(error)")
}
}
这就是我在按下编辑按钮时在 IBAction 中调用我的编辑方法的方式
let saveAction = UIAlertAction(title: "Save Changes", style: .default) { action in
let editedCategory = Category()
editedCategory.name = textField.text!
self.edit(category: editedCategory)
}
我得到的错误是:“由于未捕获的异常 'RLMException',正在终止应用程序,原因:''Category' 没有主键,无法更新'”
我已尝试在我的类别 class 中添加主键,但仍然无效。有帮助吗?
Realm 指南中涵盖了很多内容 Read and Write Data 但让我也提供一些细节。请注意,此答案反映了 Swift 10.10.0 之前的版本,并且某些内容在更高版本中发生了变化。
您尝试使用的方法
realm.add(category, update: .all)
称为 'upsert' 并且需要对象中的主键。
An upsert either inserts or updates an object depending on whether the object already exists. Upserts require the data model to have a primary key.
所以让我们重新工具化 Category 对象以包含主键 _id
class Category: Object {
@objc dynamic var _id: ObjectId = ObjectId.generate()
@objc dynamic var name: String = ""
let items = List<Item>()
override static func primaryKey() -> String? {
return "_id"
}
}
然后,当一个对象被创建时
let newCategory = Category()
newCategory.name = textField.text!
let realm = try! Realm()
try! realm.write {
realm.add(newCategory)
}
它将有一个主键
从那时起,您可以在写入中修改对象或通过它的主键更新插入它。
let someId = //the id of the newCategory created above
try! realm.write {
let anotherCat = Category(value: ["_id": someId, "name": "Frank"])
realm.add(anotherCat, update: .modified)
}
上面的代码将用新名称更新原来的 newCategory 对象,因为 _id 是相同的