как я могу переупорядочить таблицу Realm, используя tableview в swift
Я хочу переупорядочить свой список Избранное в виде таблицы в быстром темпе, используя Realm в качестве источника данных. Следующий код работает, однако он создает список избранного дважды. Я изо всех сил пытаюсь удалить данные, чтобы иметь возможность перезагрузить Избранное в правильном порядке. Вот код:
//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)
}
}
}