Reputation: 103
I am writing Todo app. My app is simple. I have two pages. One page for showing tasks in table view. And one page for adding new tasks. In this page I post to server and then in main page I get data from server and then save it in realm database.
When I Want to delete them in my main page , my app crashes by this error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TodoApp.TodosTableViewController delete:]: unrecognized selector sent to instance 0x7fca7b40b4c0
Anyone knows why? It is my deleting function :
public func delete<T:Object>(_ model: T) {
let realm = try! Realm()
try! realm.write {
realm.delete(model)
}
}
And this is the place that I use this function to delete from database :
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCellEditingStyle.delete {
rowHere = indexPath.row
// it crashes the app when I want to delete from database
// delete(self.todos[self.rowHere!])
self.todos.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)
}
}
Upvotes: 3
Views: 6699
Reputation: 741
First get Object
model in viewDidLoad()
var DatabaseModel: Results<ObjectClassRealm>!
override func viewDidLoad() {
super.viewDidLoad()
let realm = RealmService.shared.realm
DatabaseModel = realm.objects(ObjectClassRealm.self)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
let objectData = DatabaseModel[indexPath.row]
RealmService.shared.delete(objectData)
}
Or you can delete on button click
@IBAction func DeleteData(_ sender: Any) {
let realm = try! Realm()
let deleteData = realm.object(ofType: ObjectClassRealm.self,
forPrimaryKey: PRIMARYKEY)
if deleteData == nil {
print("No data in DB")
} else {
RealmService.shared.delete(deleteData!)
}
}
Upvotes: 1
Reputation: 4066
I implemented something similar in my app, hope it helps you.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//deleteRecords()
delete(index: indexPath.row)
retrieveFoods()
tableView.reloadData()
}
func retrieveFoods() {
let realm = try! Realm()
myFoodsArray = realm.objects(MyFoods.self).sorted(byKeyPath: "name", ascending: true)
}
func delete(index: Int) {
let myFood = myFoodsArray[index]
let realm = try! Realm()
try! realm.write {
realm.delete(myFood)
self.tableView.reloadData()
}
}
Upvotes: 3
Reputation: 103
I found out that that problem comes from "delete" word. It seems that this word preserved for Xcode. When I call it , it goes to another delete method in another place not mine. I changed the name to for example deleteSomethings and it worked.
Upvotes: 0
Reputation: 5303
I'm thinking that the error message is deceiving. You haven't shown where you define the class property rowHere (as an integer), but the variable isn't needed here.
delete(self.todos[self.rowHere!])
if todos is an array of your realm Object subclass, then this should work:
let objectToDelete = self.todos[indexPath.row]
delete(objectToDelete])
as long as your delete is something like this: (my Realm instance is called realmStore)
func delete(objectToDelete: Object) // use your subclass name
do {
try realmStore.write {
realmStore.delete(record)
}
} catch {
print("Could not delete object.\n\(String(describing: error.localizedDescription))"
}
Upvotes: 0