I'm using Realm for my Note-taking-app. I used core data before and I am now migrating core data to realm, but i got trouble! reordering objects like this causes error.
do {
let realm = try Realm()
let source = myNote[sour!.row]
try realm.write() {
myNote.remove(objectAtIndex: sourceIndexPath!.row)
myNote.insert(source, at: destensionIndexPath!.row)
}
}
catch {
print("handle error")
}
So I added orderPosition property to my object
dynamic var orderPosition: Int = 0
and changed tableView moveRowAtIndexPath to this ReorderingRealmResultsInTableView.swift
but it didn't helped a lot. so How can I reorder objects in realm?
I'd encourage you to store ordered items in a List
rather than sorting based on an orderPosition
property.
Storing the index manually will be much less performant when moving an item, because all objects between the "old index" and "new index" will need to be mutated to account for the change.
You can then use List.move(from:to:)
to move an object from one index to another, which should correspond directly to the indices in the table view you're reordering.
Here's a tutorial you can follow guides you through building a task management app, including support for reordering tasks: https://realm.io/docs/realm-mobile-platform/example-app/cocoa/