0

I'd like to write a function that deletes the given RealmModel from the Realm, opening and closing the Realm.

I'm having a hard time to query for the same object on the new Realm instance in this delete method. What would be the recommended way to do so? I do not even know how to get the Primary Key without manually overriding a method in each of my models.

ferbeb
  • 163
  • 1
  • 2
  • 11
  • `I do not even know how to get the Primary Key without manually overriding a method in each of my models.` that's probably because that's the way to do it. – EpicPandaForce Mar 04 '17 at 19:30
  • Any reason you cannot just use the `deleteFromRealm` method? https://realm.io/docs/java/3.0.0/api/io/realm/RealmObject.html#deleteFromRealm-- – Christian Melchior Mar 06 '17 at 07:34
  • I wanted to create a "thread-safe" delete method that would take an unmanaged object and based on the primary key, delete it from Realm. Now that I reflect on it, it seems like a quite bad idea in the first place and I should re-evaluate the scoping of my Realm instances´. – ferbeb Mar 06 '17 at 15:23

1 Answers1

0

You don't need manually implent thread-safe delete operation on your RealmModel.

You can listen for updates in your threads by Object notifications

Realm supports object-level notifications. You may register a notification on a particular RealmObject in order to be notified if the object is deleted, or whenever any of the managed fields on the object have their values modified.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60