I want to re-order my list of Favourites in a tableview in swift using Realm as the datasource. The following code works, however, it creates a list of Favourites twice. I a
The most recommended way to manage the order of a list of objects in Realm is to have another Realm object manage it as a List
property.
class FavouritesList {
let favourites = List<FavouritesRealm>()
}
This way, you can use the features of the List
property itself to manage re-ordering objects, completely within the scope of Realm.
If you don't want to go through the trouble of adding another object, another approach I've used in shipping apps before is to simply add an 'orderedIndex' property that numerically indicates the ordering of the objects, which can then sorted via adding .sorted("orderedIndex")
at the end of queries. But the first approach will be much quicker and easier to manage than this way.