2

In my app on sign up I'm checking if username is already taken.

I install the app, go to sign up, check if the username is free and everything works fine. If username is taken it tells me that. But then when I created the account and trying to create another one with the same username, for some reason this username cant be found in database, even tho it's there.

Here is the code I use:

    func singleObserveUser(withUsername username: String, completion: @escaping (UserModel) -> Void, onError: @escaping (String) -> Void)  {
        let queryUsername = username.lowercased().trimmingCharacters(in: .whitespaces)
        usersRef.queryOrdered(byChild: Constants.UserData.UsernameLowercased).queryEqual(toValue: queryUsername).observeSingleEvent(of: .value) { (snapshot) in
            if let _ = snapshot.value as? NSNull {
                onError("No userdata found")
            } else {
                if let dict = snapshot.value as? [String: Any] {
                    let user = UserModel.transformDataToUser(dict: dict, id: snapshot.key)
                    completion(user)
                } else {
                    onError("No userdata found")
                }
            }
        }
    }

If I restart app - everything is still the same. If I delete app and install it again - everything works fine.

Seams like Firebase save some data on phone.

Thank you for your help!

1 Answers1

0

If you have disk persistence enabled (the default on iOS and Android), the client stores data it has recently seen to allow it faster lookup later, and observeSingleEvent(of returns the value from that cache indeed.

If you want to ensure you have the latest value from the server, use getData instead as shown in the documentation on getting data once. Also check out Firebase Offline Capabilities and addListenerForSingleValueEvent for a longer explanation of the behavior.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807