Realm-cocoa: Object has been deleted or invalidated after remove all

Created on 12 Apr 2017  路  16Comments  路  Source: realm/realm-cocoa

Goals


Access managed object without crash

Expected Results


Access managed object without crash

Actual Results


I will remove all the datas saved in realm when user log out, however, app will crash with Object has been deleted or invalidated when log in with the same user and access data has been saved in realm. Only to access, no write.

Steps to Reproduce


No

Code Sample

Version of Realm and Tooling


Realm framework version: 2.5.1

Realm Object Server version: 2.5.1

Xcode version: 8.3

iOS/OSX version: 10.12.4

Dependency manager + version: Unknown

Reproduction-Required T-Help

Most helpful comment

Is there any update on this? I have the same issue.

  1. Log in with user X
  2. Go to particular view where Realm is used
  3. Log out and delete all data from realm (deleteAll)
  4. Log in with user X
  5. Go back to the same view
  6. Crash

All 16 comments

Hi @allsome. Thank you for reporting this but instead of writing "newest" for all of the version values, would you be able to provide us with the exact versions you're using since there are several projects in play here and they have different release cycles.

In addition to this, please provide us with a code sample and steps to reproduce the issue so our engineers can further help you.

Looking forward to hearing from you.

I will remove all the datas saved in realm when user log out, and then log in with the same user, when user perform block action, process as followd:

        var blockedNames = session.blockedUserNames
        if !blockedNames.contains(self.user.userName) {
            blockedNames.append(self.user.userName)
            session.blockedUserNames = blockedNames
        }
        Notification.blockUser.post(object: self.user)
All the above Objects are unmanaged by Realm but **self.user**, and I didn't make any change to **self.user** during the process, and post a notification finally.
    if let user = notification.object as? User {
            for data in tableView.datas {
                if let layout = data as? Layout {
                    // crash in this line 
                    if layout.status.allNames.contains(user.userName), !destroyStatuses.contains(layout.status) { 
                        destroyStatuses.insert(layout.status, at: 0)
                    }
                }
            }
        }

I will execute above code when I observe notification, App crash unexpectedly. Only status and user are managed by Realm. No writes to Realm during the whole process, and this only happens when logout and login back with the same user. All goes well when login with another user or relaunch App.

Another situation report Object has been deleted or invalidated also occur when code execute under for loop

Hi @allsome!

A Realm object can report that error either if it has been deleted from Realm, or if Realm.invalidate() was called on the Realm instance from which it was originally queried.

It sounds to me that however you're signing out, you're invalidating that object. Can you please elaborate more on how your app switches users?

On a side note, it's possible to check if a Realm object has been invalidated by checking self.user.invalidated. If that's the case, if you simply re-query for that object from Realm again, that should fix it.

In any case, please give us some more information on how your switching users. :) Thanks!

I will execute the following code to remove all objects when user logout

         try realm.write {
                realm.deleteAll()
            }

This only happen login back with the same user

Hi @allsome!

Hmm, is the object representing self.user in there too? That would certainly do it. You would need to make sure to replace self.user with a fresh copy, even if it's the same user as before.

Realm objects aren't copies of the data brought into memory; they're direct pointers to the data on disk, so if you delete the underlying data, you are unable to use any objects that were using that data beforehand.

If you're still having trouble, would you consider sending us a copy of your app's code?

If you're still having trouble, would you consider sending us a copy of your app's code?
Which part? the exact whole process has been shown as above, Realm saved each object in the background by:

try realm.write {
                realm.add(value, update: true)
            }

This only happens when logout and login back with the same user. All goes well when login with another user or relaunch App

Which part?

A full repro case would consist of an Xcode project along with clearly numbered steps that we can follow "from scratch" and hit the same issue as you're seeing.

Sorry for the delay. I think we definitely need a repro case in order to examine this issue more carefully. The code provided isn't showing enough context for what's going.

@allsome If you're okay with giving us the code to examine, please send it to [email protected] and we'll take a look at it. :)

Is there any update on this? I have the same issue.

  1. Log in with user X
  2. Go to particular view where Realm is used
  3. Log out and delete all data from realm (deleteAll)
  4. Log in with user X
  5. Go back to the same view
  6. Crash

Unfortunately no, but if you can provide us with a reproduction case the issue will get resolved sooner.

It's been a month since our last contact and we have yet to receive enough information to debug these issues, so I'm closing this. Please file new issues with steps to reproduce if you'd like some assistance.

This happens to us all the time. Our solution was to check invalidated in our database layer and to use unmanaged copies in the UI layer.

Same here
Log in with user X
Go to particular view where Realm is used
Log out and delete all data from realm (deleteAll)
Log in with user X
Go back to the same view
Crash

@dipkasyap You are commenting on a closed issue that is more than 2 years old. Please create a new issue with a minimal repro case if you think you found a bug.

@bmunkholm But it is the same issue!

We need more details. Details that are in the issue template. If you want us to look at it, please create a new issue with all details. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmorrow picture dmorrow  路  3Comments

carvalho-oak picture carvalho-oak  路  3Comments

dennisgec picture dennisgec  路  3Comments

ishidakei picture ishidakei  路  3Comments

menhui222 picture menhui222  路  3Comments