how can I re-order the Realm table using tableview in swift

前端 未结 1 1673
闹比i
闹比i 2021-01-15 19:05

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

相关标签:
1条回答
  • 2021-01-15 19:38

    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.

    0 讨论(0)
提交回复
热议问题