3

I want to add an object to favorites. In that case, when users press a button to add an object to favorites, the object will be saved to realm. then he press a button to remove the object from favorites and it will be removed. Afterwards, he press the button to save again, it should be saved again. Is it available?

When I pressed the save button twice, it says

Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'

The codes are like below.

func delete() {
realm.write({
    self.realm.delete(obj)
})
}
func save() {
    realm.write({
        self.realm.add(obj, update: false)
    })
}
user3225917
  • 2,991
  • 2
  • 13
  • 17
  • possible duplicate of [Realm crashes with RLMException: object has been deleted or invalidated](http://stackoverflow.com/questions/29432191/realm-crashes-with-rlmexception-object-has-been-deleted-or-invalidated) – Anbu.Karthik Aug 31 '15 at 10:50

1 Answers1

6

(Disclaimer: I work for Realm)

Nope! Like it says on the tin, when an object has been deleted from a Realm, you can't simply use the same object reference to add it again.

It might be necessary to re-think your logic there. If the user has the opportunity to re-add the object after they've moved to delete it, it might be a good idea to not actually delete it, until the user has progressed the app to the point where they can't go back.

Failing that, you can always just save the information in the object, and simply create a new object when the user presses the button again.

TiM
  • 15,812
  • 4
  • 51
  • 79
  • >Failing that, you can always just save the information in the object, and simply create a new object when the user presses the button again. How can I do that? Realm allows us to copy the object? – user3225917 Sep 01 '15 at 00:41
  • If you need to quickly restore the file, it would make sense to 'hold on' to it, (Even if the UI reports it's deleted) until the user truly doesn't need it anymore. – TiM Sep 01 '15 at 02:41
  • No, by that, I meant you would have to hold onto the information to recreate that object from scratch. Obviously that's a lot more trouble and effort, which is why I recommended the 'tentatively deleted' option above. – TiM Sep 01 '15 at 02:42
  • I see. The tentative solution is available for deleting messages, accounts and threads. But I think the way is not suitable for favorites. How do you think the design of Realm about this? – user3225917 Sep 01 '15 at 03:01
  • Hrmm okay! I think the behavior of Realm in this case is working as intended. Once you've called delete on an object, and Realm has completely evicted it from its store, it doesn't make sense to have additional functionality where you might want to optionally 'undo' this operation. I definitely think there's a solution here that doesn't involve explicitly deleting the object from Realm until it's absolutely necessary. – TiM Sep 01 '15 at 03:14
  • I ran into the exact same problem - I have a collection of search results in my main ViewController with option to "favour" some results. I have another tab which shows only the favourites, and an option to remove some of them. When I return to the 1st tab and try to re-add the same object, I used to get the "invalidated" error. How I solved this: each time I return to the main tab, I redo the search query, re-creating the search results collection, so I have brand new objects to work with. Do you think this is a good solution? – MayaLekova Feb 13 '17 at 11:33
  • @MayaLekova Hi Maya! Hmm, that sounds odd. Realm objects aren't marked as 'invalidated' unless they're deleted from disk (or if you explicitly called `invalidate` on its parent Realm). Realm objects are 'live' (eg, they're pointers straight to the data on disk, not copies in memory), so re-querying like that shouldn't change anything. It sounds like there might be more going on in your code here. I'd recommend starting a new question and showing your code so we can explore it in more detail. :) – TiM Feb 13 '17 at 18:18