如何快速使用TableView重新排序Realm表 [英] how can I re-order the Realm table using tableview in swift

查看:93
本文介绍了如何快速使用TableView重新排序Realm表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Realm作为数据源快速重新排列我的收藏夹列表在表视图中的位置.下面的代码有效,但是,它两次创建了一个收藏夹列表.我正在努力删除数据,以便能够以正确的顺序重新加载收藏夹.这是代码:

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 am struggling to delete the data in order to be able to re-load the Favourites in the right order. Here is the code:

//MARK:REORDER list
override func tableView(tableView: UITableView, var moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let favouritesR = realm.objects(FavouritesRealm)

    //convert to array
    favouritesArray = []
    for min in favouritesR{
        favouritesArray.append(min)
    }

    try! realm.write {
        //move the row
        let movedObject = self.favouritesArray[sourceIndexPath.row]
        favouritesArray.removeAtIndex(sourceIndexPath.row)
        favouritesArray.insert(movedObject, atIndex: destinationIndexPath.row)

        //here I would like to clear the FavouritesRealm table of all data, so that with the below code I can just read the itmes back in the right order. However, deleting rows crashes the app. See my previous question http://stackoverflow.com/questions/38068826/why-does-clearing-contents-of-realm-table-invalidate-the-object/38095648#38095648

        //read the re-ordered list back into the FavouritesReam table
        for f in favouritesArray{
            let favouriteRealm = FavouritesRealm()
            favouriteRealm.name = f.name
            favouriteRealm.price = f.price
            favouriteRealm.dbSource = f.dbSource
            favouriteRealm.date = f.date
            favouriteRealm.favourite = f.favourite
            realm.add(favouriteRealm)
        }
    }
}

推荐答案

在Realm中管理对象列表顺序的最推荐方法是让另一个Realm对象作为List属性对其进行管理.

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>()
}

这样,您可以使用List属性本身的功能来完全在Realm的范围内管理对象的重新排序.

This way, you can use the features of the List property itself to manage re-ordering objects, completely within the scope of Realm.

如果您不想麻烦地添加另一个对象,那么我之前在运送应用程序时使用的另一种方法是简单地添加一个'orderedIndex'属性,该属性以数字方式指示对象的顺序,然后可以通过在查询末尾添加.sorted("orderedIndex")进行排序.但是第一种方法比这种方法更快捷,更易于管理.

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.

这篇关于如何快速使用TableView重新排序Realm表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆