如何将0索引处的项插入到Realm容器中 [英] How to insert items at 0 index to the Realm container
问题描述
有没有办法在0索引处插入新项目到 Realm
容器?我没有在 Realm
类中看到插入方法。
Is there a way to insert new items at the 0 index to the Realm
container? I don't see an insert method in the Realm
class.
我需要使用列表
s吗?如果答案是肯定的,我如何重构以下代码以便能够使用 List
并使List与 Realm <保持同步/ code>容器。换句话说,我很难想出一个很好的方法来保持
Realm
容器和 List
添加和删除时使用相同的项目。
Do I need to use List
s? If the answer is yes, how can I restructure the following code to be able to use List
s and keep the List in constant sync with the Realm
container. In other words I'm having a hard time coming up with a good way to keep the Realm
container and the List
with the same items when adding and removing.
在以下代码中,在最后一个索引处输入新项目。如何重组它以便能够在0索引处插入项目?
In the following code new items are entered at the last index. How can I restructure it to be able to insert items at the 0 index?
import RealmSwift
class Item:Object {
dynamic var productName = ""
}
主ViewController
Main ViewController
let realm = try! Realm()
var items : Results<Item>?
var item:Item?
override func viewDidLoad() {
super.viewDidLoad()
self.items = realm.objects(Item.self)
}
func addNewItem(){
item = Item(value: ["productName": productNameField.text!])
try! realm.write {
realm.add(item!)
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell", for: indexPath)
let data = self.items![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 = items?[indexPath.row] {
try! realm.write {
realm.delete(item)
}
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}
}
理想情况下,这是我希望在插入新项目时能够做到的addNewItem()方法......
Ideally this is what I would like to be able to do when inserting new items in the addNewItem() method...
item = Item(value: ["productName": inputItem.text!])
try! realm.write {
realm.insert(item!, at:0)
}
推荐答案
添加一个 sortedIndex
整数属性,让你手动控制对象的排序绝对是最受欢迎的订购方式之一在Realm中的对象,但它效率很低。为了将对象插入0,您需要遍历每个其他对象并将其排序数增加1,这意味着您最终需要触摸数据库中该类型的每个对象才能执行此操作。
Adding a sortedIndex
integer property that lets you manually control the ordering of objects is definitely one of the more popular ways to order objects in Realm, however it's quite inefficient. In order to insert an object at 0, you'll then need to loop through every other object and increment its ordering number by 1, meaning you'll end up needing to touch every object of that type in your database in order to do it.
这种实现的最佳实践是创建另一个 Object
模型子类,其中包含列出
属性,在Realm中保留它的一个实例,然后将每个对象添加到它。 List
属性的行为与普通数组类似,因此可以非常快速有效地安排对象:
The best practice for this sort of implementation is to create another Object
model subclass that contains a List
property, keep one instance of it in Realm, and to then add every object to that. List
properties behave like normal arrays, so it's possible to very quick and efficient to arrange objects that way:
import RealmSwift
class ItemList: Object {
let items = List<Item>()
}
class Item: Object {
dynamic var productName = ""
}
let realm = try! Realm()
// Get the list object
let itemList = realm.objects(ItemList.self).first!
// Add a new item to it
let newItem = Item()
newItem.productName = "Item Name"
try! realm.write {
itemList.items.insert(newItem, at: 0)
}
然后,您可以直接使用 ItemList.items
对象作为表视图的数据源。
You can then use the ItemList.items
object directly as the data source for your table view.
这篇关于如何将0索引处的项插入到Realm容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!