Swift SDK crash when deleting user

I am trying to add the delete user functionality for iOS app as this is requirement for App Store approval.

I am crashing when I make the delete call, which succeeds in deleting the user. Code for delete below:

private func delete(user: User) async {
    do {
        try await user.delete()
        print("Successfully deleted user")
    } catch {
        print("Failed to delete user: \(error.localizedDescription)")
        self.errorMessage = ErrorMessage(errorText: error.localizedDescription)
    }
}

The output of the console for crash:
Successfully deleted user
libc++abi: terminating due to uncaught exception of type realm::app::AppError: Cannot start a sync session for user ‘6627f075da767128555c229c’ because this user has been removed.

The calling code to delete is:
Button(action: {
guard let user = realmApp.currentUser else { return }

                let subscriptions = realm.subscriptions
                subscriptions.update {
                    subscriptions.removeAll()
                }
                
                Task {
                    await delete(user: user)
                }
            }) {
                Text("Delete Account")
            }

Not that I unsubscribe for any updates I’m currently receiving though I don’t know if that is needed.

I suspect this has something to do with deleting the user I am logged in as? But if I log the user out first before deleting him/here the delete function fails since I need to be logged in to make that call.

Is there anything I’m missing here or that I should check? This is one of the final pieces I need to complete before submitting to Apple. I have searched here and stack overflow, but could not find anything helpful.

Same issue over here. Any suggestions?

I think this was fixed in 10.50.0.

*Deleting the active user (via User.delete()) left the active user
unset rather than selecting another logged-in user as the active user like
logging out and removing users does. (Core #7300, since v10.23.0).

I think what this is saying is that the bug which was fixed in the update was that the user was still “logged in” but there is no user, so that’s why it was crashing.

The other option / workaround I’ve implemented is creating a function on App Services where the user is deleted. Then on the client I call the function and then log out the user without awaiting it.

@kurt_Libby1 Thank you for the response.

I try to update the library regularly but didn’t realize there was an update. I updated [realm-core-14.5.2] → [realm-core-14.6.2]. I’ve added myself on github for notifications when the library is updated so I can get notice of any updates.

I now call logout() and then delete() client side and this seems to be working. I was doing this prior to the update you referenced, but it wasn’t working on the older version.

I was aware you could do this server side, but I am primarily mobile developer so it’s easier/better to do client side. The on how to properly delete a user need to be updated if they haven’t already. It’s a requirement of Apple for 100% of all apps that they provide the ability to delete a user and their data.