wie kann ich die Realm-Tabelle mit tableview in swift @ nachbestelle

Ich möchte meine Favoritenliste in einer Tabellenansicht mit Realm als Datenquelle schnell neu anordnen. Mit dem folgenden Code wird jedoch zweimal eine Liste der Favoriten erstellt. Ich habe Probleme, die Daten zu löschen, um die Favoriten in der richtigen Reihenfolge neu laden zu können. Hier ist der 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)
        }
    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage