Firebase realtime database query doesn't get new data until app reinstalled - swift

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!

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.

Related

Error when saving to keychain using SecItemAdd

I'm getting an error saving an encoded value to keychain at the point of SecItemAdd. I'm fairly new to working with Keychain and not sure how to return the error to see what I'm doing incorrectly.
let encoder = JSONEncoder()
func initiateLogin(forceReconnect: Bool = false, completion: #escaping (Bool)->Void) {
Task {
await loginUser(forceReconnect: forceReconnect, completion: { user in
if let encoded = try? self.encoder.encode(user) {
// MARK: - keychain
let attributes: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: "johnDoe",
kSecValueData as String: encoded,
]
if SecItemAdd(attributes as CFDictionary, nil) == noErr {
print("\(#function) 😀 user saved successfully in keychain")
} else {
print("\(#function) ⚠️ something went wrong")
}
self.initClient(withCredentials: user)
completion(true)
}
})
}
}
You didn't specify which error you are getting (it should be a return value of SecItemAdd), but the most common mistake is this: as documentation states:
The operation might fail, for example, if an item with the given attributes already exists.
In other words: your code will only work once for each unique kSecAttrAccount.
Instead, you need to check if an item already exists, and if yes, update it (or delete the previous one and create a new one).
How to update the items or delete them is explained here.
Side note: it's also a good idea to put keychain management into a separate class (a wrapper), which you can call from anywhere in your code to save / load data from keychain. Here's a good tutorial on how to create such wrapper.

IOS Firebase. User UID changing after restarting the application

If i understood correct the user UID its this is a unique uid, until the user logs out. I mean he can close/open the app many times and user UID must be the same.
I have test class:
class UserFirebase {
func authorization(completionHandler: #escaping (Result<AuthDataResult?, Error>) -> Void) {
Auth.auth().signInAnonymously { authResult, error in
if error == nil {
completionHandler(.success(authResult))
return
}
completionHandler(.failure(error))
}
}
func singOut() {
try? Auth.auth().signOut()
}
func getUserUUID() -> String? {
return Auth.auth().currentUser?.uid
}
func isAuthorized() -> Bool {
return Auth.auth().currentUser != nil
}
}
when app is running i using this class like this:
let userFirebaseManager: UserFirebase = UserFirebase()
if userFirebaseManager.isAuthorized() {
// make something
} else {
userFirebaseManager.authorization(completionHandler: {[weak self] (result) in
// make something
})
}
every time I start the app, the user isAuthorized() == false. Shouldn't it persist until I press logout?
UPD:
why does my currentUser.uid change every time I restart the application?
The UID of an anonymous user is deleted when the user signs out. It cannot be regained after that, not even when you sign in on the same device.
Also see:
How constant is the Firebase Anonymous ID
Firebase Anonymous Authentication
Firebase Anonymous Auth: Restore account
How to logout and then re authenticate an anonymous firebase user
Anonymous users are signed out once the app is closed. I'm not sure why you need to sign in anonymously AND keep that UUID known. If you need to sign in anonymously just to securely access your Firebase project, it shouldn't matter if the user gets signed out. If you want to preserve the UUID, the user needs to create an account with Auth.auth().createUser(withEmail: String, password: String, completion: ((AuthDataResult?, Error?) -> Void)

Swift Firebase 4.0 - observeSingleEvent not returning data set (but authentication is working)

I have been working with Firebase on Android for the last 12 months+, with success. I just switched over to Swift and am attempting to read data from the same Firebase database I created and have been using the last 12 months+. Since there is security applied to the FB DB the first thing I did was to get FB authentication (from Swift) working. This works. Now, I am trying to get a simple observeSingleEvent operational and am struggling.
The Firebase DB is the same old stuff. It has a users node off of the root. For starters I would just like to read in a user from the user's node. I know authentication is working because when I submit my email and password I receive a confirmation. When the email / password are wrong I do not get confirmation. Given this validates my connection to the DB and as a test I stuck the following code in right after login validation.
When I debug this it simply skips from the "self.ref?.child("users").observeSingleEvent..." to the bracket below. i.e. It never acknowledges there is data available, but there is data available.
To avoid anyone asking "What do you need?" What I am looking for is an answer to why I receive no data result set with the code below given there is data in the FB DB and I have been reading/writing that data on Android for the last 12+ months.
Any/all help is welcome as I cut my teeth on Swift / FB 4.0
#IBAction func signInButtonTapped(_ sender: UIButton) {
// TODO: Do some form validation on the email and password
if let email = emailTextField.text, let pass = passwordTextField.text {
// Check if it's sign in or register
if isSignIn {
// Sign in the user with Firebase
Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't nil
if let u = user {
// User is found, go to home screen
self.ref = Database.database().reference()
self.ref?.child("users").observeSingleEvent(of: .value, with: {( snapshot) in
let value = snapshot.value as? NSDictionary
let username = value?["username"] as? String ?? ""
print ("*** " + username)
})
}
else {
// Error: check error and show message
}
})
}
Since observeSingleEvent is an async call so it is running on the background thread, that is why it jumps on to the next bracket when you put a break point on self.ref, because it doesnt block the current thread of execution and does execute after sometime on background thread
One way to do is this:
#IBAction func signInButtonTapped(_ sender: UIButton) {
// TODO: Do some form validation on the email and password
if let email = emailTextField.text, let pass = passwordTextField.text {
// Check if it's sign in or register
if isSignIn {
// Sign in the user with Firebase
Auth.auth().signIn(withEmail: email, password: pass, completion: { (user, error) in
// Check that user isn't nil
if let u = user {
// User is found, go to home screen
self.fetchUserName { fetchedUserName in
print(fetchedUserName)
}
}
else {
// Error: check error and show message
}
})
}
func fetchUserName(completionHandler: #escaping (String) -> Void) {
self.ref = Database.database().reference()
self.ref?.child("users").observeSingleEvent(of: .value, with: {( snapshot) in
//let value = snapshot.value as? NSDictionary
if let value = snapshot.value as? [String: Any] {
print(value)
let username = value["username"] as? String ?? ""
print (username)
completionHandler(username)
}
})
}

When retrieving data from Firebase Database, <null> is returned: "Snap (...) <null>"

I'm a relatively new Swift programmer and am using Firebase for the first time so please excuse any misunderstandings I may have and my lack of knowledge about terminology.
I am attempting to retrieve data about a user that is stored in a database (email and username).
The code successfully finds the userID in the database. The userID is then used in order to navigate into the directory containing the username and email. It stores those values in snapshot.
For some reason, when snapshot is printed, it shows the userID but the contents of the directory (username and password) are shown as <null>. I am certain that the directory I am attempting to access and retrieve data from exists and is not empty (it contains a username and email). I wantsnapshot to store the username and email, but printing shows that it is not doing so correctly and I cannot figure out why.
here is my code block:
func checkIfUserIsLoggedIn() {
if Auth.auth().currentUser?.uid == nil {
perform(#selector(handleLogout), with: nil, afterDelay: 0)
} else {
let uid = Auth.auth().currentUser?.uid;
Database.database().reference().child("Users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
print(snapshot)
if let dictionary = snapshot.value as?[String:AnyObject] {
self.userLabel.text = dictionary["name"] as? String
}
}, withCancel: nil)
}
}
and here is what is being printed to the console:
Snap (ywU56lTAUhRpl3csQGI8W8WmQRf1) <null>
Here is the database entry I am attempting to reach and log to snapshot:
I'm a new Stack Overflow user and don't have enough experience on the site to be allowed to embed images in posts, so this is the external link
Thanks for reading, any help would be much appreciated!!
Your reference in Firebase is to "users", but you are using .child("Users") in your code. Make sure your lookup matches case to your node. I find it best to create a reference to that node and use it for writing to and reading from.
let usersRef = Database.Database().reference().child("users")
Snap (ywU56lTAUhRpl3csQGI8W8WmQRf1) <null> the portion in parenthesis refers to the end node of what you are trying to observe. In this case it refers to uid!.
if u want to get username or email then you make first the model class for
Example:-
class User: NSObject {
var name: String?
var email: String?
}
then user firebase methed observeSingleEvent
FIRDatabase.database().reference().child("user").child(uid).observeSingleEvent(of: .value, with: { (snapShot) in
if let dictionary = snapShot.value as? [String: Any]{
// self.navigationItem.title = dictionary["name"] as? String
let user = User()
user.setValuesForKeys(dictionary)
self.setUpNavigationBarWithUser(user: user)
}
})`
if it is not finding your asking values, you are asking wrong directory. check firebase db child name it must be exactly like in your code ("Users")

Firebase: How to get User.uid?

I'm using Firebaseauth to manage users and data for my iOS app. The users can log in, and their userinfo is stored correct in the database.
But when I try to get the users to write to the database in a different viewController, using this string:
self.ref.child("users").child(user.uid).setValue(["username": username])
The thrown error is
Type user has no member .uid
That makes sense I guess, since I haven't created the variable. But I can't figure out how to declare it?
This is how you get the user's uid:
let userID = Auth.auth().currentUser!.uid
UPDATE: Since this answer is getting upvotes, make sure you prevent your app from crashing by using guards:
guard let userID = Auth.auth().currentUser?.uid else { return }
You might want to use a guard, as a force unwrap might break your app if a user isn’t signed in.
guard let userID = Auth.auth().currentUser?.uid else { return }
FIRAuth has actually been renamed to Auth. So, as per the latest changes, it will be
let userID = Auth.auth().currentUser!.uid
Edit.
As pointed out in comments, A force unwrap may crash the app.
guard let userID = Auth.auth().currentUser?.uid else { return }