I have a table for favourites and what I want to do is to clear the table of all data and then reload it with the contents of an array. Here is the code:
//empty FavouritesRealm table and reload favouritesArray back into FavouritesRealm
let clearTable = realm.objects(FavouritesRealm)
try! realm.write{
for row in clearTable{
realm.delete(row)
}
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)
}
}
Now, the app crashes with the comment: "Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'"
Swift seems to delete my object (which is the table) when all rows are deleted, but I just want to clear all data. How can I get around this?