Error with next login after deleting a user

Hello,

Registering and logging in as an email/password user for the most part works fine in my SwiftUI app that uses Flexible Sync, but something goes wrong if I try to create and log in as a new user right after deleting a different one. Specifically, if I don’t manually close and reopen my app before creating a new user after having deleted one, I get this error when loading the new user:

terminating with uncaught exception of type realm::RealmFileException: Directory at path ‘/var/mobile/Containers/Data/Application/7377FD0A-6C34-4BD2-901F-E7681014E17A/Documents/mongodb-realm/app-id/62e21a1622edb7644cb9d1ff/flx_sync_default.realm.lock’ does not exist.

If on the other hand I relaunch my app manually before trying to create the new user after deleting one, there’s no issue, so for some reason things don’t seem to be resetting correctly on their own without an app refresh.

I use app.currentUser?.delete() to delete a user, and I can confirm that it gets removed from the user list in my app’s dashboard online. Also, I’m not sure if this is useful to know, but there’s no similar issue if I simply log out as a user and log in as a different one. It only happens with user deletions.

Any help on interpreting the error would be much appreciated!

Thank you,
Jack

Can you include the complete code you’re using to delete the user from the app?

Hi Jay,

I call this function to delete a user’s account:

func deleteAccount() async {
    do {
        try await app.currentUser?.delete()
        print("Account deleted")
    } catch {
        print("Unable to delete account: \(error)")
    }
}

Should the flx_sync_default.realm.lock file be getting automatically deleted when I delete a user? Based on the path in the error message it would make sense that it’s no longer needed (62e21a1622edb7644cb9d1ff was the ID of the deleted user), but the app doesn’t seem to agree haha.

Here’s some datapoints after testing so let me set this up

I have (macOS) app TaskTracker that with button press, can log in anonymously (or with user). When that happens a new folder is created on the local drive

then, I have a button action that calls a function

@IBAction func testButton1Action(_ sender: Any) {
    print(#function)

    Task {
        await self.handleDeleteUserAsync()
    }
}

to delete the user using the same code you’re using (note that gTaskApp is a “global” var that points to my app)

func handleDeleteUserAsync() async {
    do {
        try await gTaskApp.currentUser?.delete()
        print("Account deleted")
    } catch {
        print("Unable to delete account: \(error)")
    }
}

When that happens, it deletes the folder 62e6…

and logs the user out and prints Account deleted in console - the .lock file is deleted because everything, including the parent folder is deleted.

So in other words, I can’t duplicate the issue. :thinking:

This behavior appears to be identical between a partition based and flex app as it involves authentication only so the sync type doesn’t seem to have an impact. I could be incorrect on that assumption.

Thank you for testing this out. The issue with my app seems to arise after the process you describe though. After you delete a user and it returns you to your login screen, does it let you log in with a new user without any issue? It’s when I attempt to log in with a user right after deleting a different one that my app crashes.

The .lock file seems to be getting deleted like in your case, it prints “Account deleted,” and it takes me back to my login screen because my app variable is monitored to allow the app to recognize that there is no longer a user logged in by checking

if let user = app.currentUser {
    ... // Flexible Sync config setup
    OpenSyncedRealmView()
        .environment(\.realmConfiguration, config)
} else {
    LoginView()
}

It’s just that my app then tries to find the deleted .lock file for the old user instead of moving on to deal solely with a newly logged in user if I don’t first close and reopen my app before logging in with a new one. It’s like the app isn’t refreshing correctly on its own. Is there some call I need to make to refresh the realm app manually after deleting a user? I’m using AsyncOpen in my OpenSyncedRealmView to open a realm in a way that’s almost identical to the SwiftUI Quickstart tutorial btw (https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/), and it’s when it gets back to that point with a new user that the crash seems to occur.

Yes. I can create new users or anonymous and login, delete the user and immediately create and log in with a new user or anonymous account without exiting the app.

We have a class var that stores the task results so we nil that after deleting a user and then refresh the task tableview to clear it.

Perhaps you’re holding a strong ref the user somewhere?