How to force close Realm database with Swift?

According to the documentation, close is not available in Swift SDK.

There is no need to manually close a realm in Swift or Objective-C. When a realm goes out of scope and is removed from memory due to ARC , the realm is closed.

However when handling a user logout use case, after the user logged out and try to log in again the Realm through a “Realm at path XXX already opened with different sync user.”

Apparently user.logOut would not close the Realm for me and I do not think I have any previous Realm ref stored in the memory.

I have even tried to delete the Realm database file associated with the sync user but once the user got re-logged into the app. The same error appears.

The error seems to be caused by a different sync config between the local realm and sync realm, so I checked the sync config content; however, the user ID, partition, and a few other fields are all the same because they are both from the same sync user.

Tried both .logout and .remove and both methods turn out to have the same issue after the same user re-logs in after signing out in the same app session.

Seems like you have a reference to your Realm object somewhere, maybe a singleton or a zombie object. What does xcode debug memory graph show you? Do you see any Realm object there?

Hi @Jerome_Pasquier thanks for the response. That’s what I suspect is happening right now. I am new to iOS dev so I could not find where is the Realm object reference in my code. Thank you for the pointer, I can check the memory graph and see if I could find the bug there.