Scaling with Firebase - ios

I'm new to storing and retrieving large amounts of data from a database. I have an iOS app that stores data with Firebase and everything is working fine, but I'm worried that once there's more and more data, performance will suffer.
For example, when creating a profile, the user must choose a username that has not already been taken. In order to do this, I retrieve all of the existing usernames and check if the new username is already there. My question then is how can I test what will happen if there are thousands or even millions of existing usernames?
Thanks in advance.

Root
Profiles
[username1]
...
[username2]
...
[username3]
...
let ref = FIRDatabase.database().reference().child("Profiles").child("\(username)")
ref.observeSingleEvent(of: .value, with: {snapshot in
if snapshot.exists() {
print("user exists")
} else {
print("user not exists")
}
})

Related

firebase real time dB retrieving of specific data from multiple users in swift

so I'm working these days on a new project and I have a problem I can't solve, I hope someone can help me.
I'm working on an iOS app, I'm storing all user data on Firebase Real time dB.
My main goal is to get specific data from all users from particular positions,
For example:
Inside users, I have different UIDs of all the users in the dB.
In each one of them, there is a username, I would like to retrieve the username of each user.
In the future, I would like to store the location for each user under "Location". and then I would like to get all users that their location is "New-York".
I'll be glad to get some ideas on how to figure it out!
Thanks!
users
XLS37UqjasdfkKiB
username: "Donald"
ei8d4eYDafjQXyZ
username: "Barak"
etcj0lbSX5Oasfj
username: "Abdul"
rglmlG6Rasdgk5j
username: "Ron"
You can:
Load all JSON from /Users.
Loop over snapshot.children.
Then get the value of each child's username property.
These are also the most common navigation tactics when it comes to Firebase Realtime Database, so you'll apply these to pretty much any read operation.
Something like this:
Database.database().reference().child("isers").observe(.value, with: { (snapshot) in
if !snapshot.exists() {
print("No users found")
} else {
for case let userSnapshot as DataSnapshot in snapshot.children {
guard let dict = userSnapshot.value as? [String:Any] else {
print("Error")
return
}
let username = dict["username"] as? String
print(username)
}
}
})
Also see:
the documentation on reading data.
the documentation on listening for lists of data with a value event.
How to properly use queryOrderedByValue
Get Children of Children in Firebase Using Swift
And more from searching on [firebase-realtime-database][swift] children.

Issue Pulling UID from Firebase in Swift

I've seen many youtube videos and previously answered questions on this exact subject, but for some reason my code is not working.
I want to display the user's name that is stored, but I can't pull the UID from FireBase. At this point, the user should be logged in so I don't think it's an issue of forcing the UID. Any help would be great.
Code
Firebase Data
You can get your uid as:
var ref : DatabaseReference = Database.database().reference().child("klaw-unpw").child("users")
And in your viewdidload add this code:
ref.observe(.value, with: { (snapshot) in
print(snapshot.value)
}) { (error) in
print(error)
}
But it will be good if you will start your structure with "users" instead of "klaw-unpw".
Hope it will work for you.

Removing a specific value off of firebase database

I am setting up a social media app and currently working on a way to create authentic usernames(no duplicates) for the user to enter, similar to that of Instagram.
The problem that I'm facing is that I can't find a way to delete the users previous username(in case anyone else wants to use it). The way that my database is setup for usernames is like:
Usernames
- username:"testUsername"
I have attempted to delete the code using this
let usernameRef = FIRDatabase.database().reference().child("Usernames").child("username").child(usersCurrentUsername)
usernameRef.removeValue(completionBlock: {(error, ref) in
if error != nil {
print("There was an error in removing the current username\(error?.localizedDescription)")
} else {
print(ref)
print("The child was removed")
}
})
I capture the users current username via snapshot in the viewdidload and store it in usersCurrentUsername.
Any help would be appreciated
let usernameRef = FIRDatabase.database().reference().child("Usernames").child("username");
usernameRef.removeValue();
Note that if the child of Usernames is only the username, the Usernames node will also be deleted.

How to get specific info for all users?

How can I get the kaarsen array out of my Firebase database of every single user?
You seem to be nesting different types of data, which is a big anti-pattern in the Firebase Database. When you read data from Firebase, you always read entire nodes. So in your scenario you either read the entire user, or you don't read them. You cannot retrieve just the kaarsen node for each user. This is one of the many reasons why Firebase recommends against nesting different types of data.
In your case it seems best to split the kaarsen int a top-level node:
users
<uid>
email: ...
geboortedatum: ...
naam: ...
kaarsen
<uid>
...
With this structure you can get the kaarsen for all users by accessing /kaarsen.
let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("users").child(userID!).child("kaarsen").observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as! Array
arrayOfKarsens.append(value)
// ...
}) { (error) in
print(error.localizedDescription)
}

Adding users to firebase

I'm having a bit of trouble adding users to Firebase. If anyone could help me out with this, it would be significantly appreciated.
Here is my code:
var myRootRef = Firebase(url:"https://plattrapp.firebaseio.com/users")
myRootRef.createUser(emailSignUpEntered, password: passwordSignUpEntered,
withValueCompletionBlock: { error, result in
if error != nil {
// There was an error creating the account
} else {
let uid = result["uid"] as? String
println("Successfully created user account with uid: \(uid)")
}
})
It does display in the println statement within my debugger that a user has been created, but doesn't actually display within my firebase database.
Anything I may be doing wrong?
Firebase Authentication does not automatically create any information about the user in the associated database.
Most applications end up creating this information from their own code under a top-level users node. This is covered in the section called "Storing User Data" in the Firebase programming guide for iOS.
It is in general a good idea to read the Firebase documentation on the topic that you are working on. It will prevent a lot of grey/lost hair and wasted time.

Resources