Firebase Android showing and modify element per single User [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm making this android app using Firebase where I have some books and every user user can set them as read or reading.
I don't know how to do it since Firebase is noSQL and there are no Relations between entities.
There are these three activities: in the first one the user can see all the books and eventually set one as read or reading, in the second there are only books marked ad read and in the third the books marked as reading.
They must be separated, every user has its own.
How can I do this using Firebase realtime database and storage?

I'd likely create two top-level nodes, for the two types that you have:
Users
Books
And then create two additional top-level nodes for the relations between these:
UserBooks - which is where we tie each user to their books, and track their reading status.
BookUsers - which ties each book to the users. You only need this if you want to show the users for a specific book, but I typically recommend adding it right away.
So a simple model like this could look like:
Users: {
"uidOfFio": { ... },
"uidOfPuf": { ... }
},
Books: {
"idOfBook1": { ... },
"idOfBook2": { ... }
},
UserBooks: {
"uidOfFio": {
"idOfBook1": "Read",
"idOfBook2": "Reading"
},
"uidOfPuf": {
"idOfBook1": "Reading",
}
},
BookUsers: {
"idOfBook1": {
"uidOfFio": "Read",
"uidOfPuf": "Reading"
},
"idOfBook2": {
"uidOfFio": "Reading"
},
}
For more on this, I recommend reading:
Many to Many relationship in Firebase
Firebase query if child of child contains a value

Related

Make buttons disappear due to user roles (kotlin) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is it possible to make a button disappear in my app when a user is a viewer? I have 3 types of users (viewer, editor and admin) and I want the same layout for the 3 of them but, the viewer cannot have access to some buttons. Is it possible to make the buttons disappear or should I make the exact same layouts but without the buttons? Thanks!
edit
here's a screenshot of firebase database
The roles are stored in realtime database so you would have to first read that and then hide the buttons based on the role. Right now you are comparing the UID of user with the role "Viewer" - if(FirebaseAuth.getInstance().currentUser!!.uid == "Viewer")
val user = Firebase.auth.currentUser
val uid = user.uid
mDatabase.child("users").child(uid).child("Role").get().addOnSuccessListener {
Log.i("firebase", "Got role ${it.value}")
if (it.value == "Viewer") {
newDashButton.visibility = View.GONE
}
}.addOnFailureListener{
Log.e("firebase", "Error getting data", it)
}

How do I see all my data that has been read in Cloud Firestore? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have successfully read all my user data into cloud firestore but unable to see all my data in one go.
As you can see in the screen capture, "Sara" has been read. Beforehand, "Mira" and "Kiena" has also been read.
How do I see "Mira" and "Kiena" together with "Sara" in one screen? I want to see all the data that has been read in Cloud Firestore.
I need to have all this as a record for my thesis.
I set the user data with this code:
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("sampleData/userdata");
You need to add this
SetOptions.merge()
inside the set function in your code
what is happening now is that firestore takes a key = name and values = sara/mira/etc and keeps overriding the values since the key is the same, but when you use the merge options it will not override and will display all in a list
It looks like you're setting the user data with something like:
db.collection("sampleData").document("userData").set(...)
Any time you execute this code, you're overwriting whatever data already exists in userData.
If you want to create a new document each time you run the code, do:
db.collection("sampleData").add(...)
This will generate a new document with a unique ID each time you execute it.
If the document needs to be associated with the current user from Firebase Authentication, you'll want to use the user's UID as the document name. That'd look like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("sampleData").document(uid).set(...)

How would you structure mutual rating social network model on Firestore? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I think it would be easier to display my model with an example. The functionality of the app would be the following: It would be social network schema. A mutual friend model. When a user logins in the app, a list will be displayed. Each list item consist of a friend name - photos(photo1, photo2) - rating for each photo(calculated based on both ratings).
Example:
Logged in as UserA:
list item1:
[
name: UserBName,
photo1: 5 (UserA rated it 2, UserB rated it 8),
photo2: 4 (UserA rated it 3, UserB rated it 5)
],
list item2:
[
name: UserCName,
photo1: 2 (UserA rated it 1, UserC rated it 4)
]
I was thinking of creating a friend subcollection for each user document and inside having a structure like that:
UserB: {
name: UserBName,
photo1:{
rating from UserA: 4
rating from UserB: 2
},
photo2:{
rating from UserA: 3
rating from UserB: 5
}
}
My issue is that UserB(document)->friends(subcollection)->UserA(document) would have the same data and i am not so sure if its a viable solution.
Duplicating data and denormalizing data is common practice in NoSQL databases. It's used to flatten structures so queries can be run against the data. In the question we don't know what kind of queries you want to run other than
When a user logins in the app, a list will be displayed. Each list
item consist of a friend name - photos(photo1, photo2) - rating for
each photo(calculated based on both ratings).
Which doesn't require a query. Using the structure in the question, when UserB logs in, your app will read the UserB document which will contain their name and all of their photo's and ratings so all of that data will be available as soon as the document is read.
However...
If each user has LOTS of photos's that could be a LOT of data to be read in and could overwhelm the device. Additionally, if you are really trying to avoid duplicating data, then you need to put the data in one place.
I would change the structure completely to provide more flexibility in only reading in the data you want.
First a simpler user collection: the documentId's are the users uid and then store their name etc as field within each document
users
uid_0:
name:
uid_1:
name:
Then a photos collection that stores the ratings
firestore_root
photos
photo_0
uid_0: 4
uid_1: 2
photo_1
uid_3: 2
uid_2: 5
photo_2
uid_0: 3
uid_1: 1
So then when a user logs in, perform a query on the photo's node for all photos that contain a users uid with a rating > -1 (assuming ratings go from 0 to 5 for example). In this case that would be photo_0 and photo_2
in pseudo-code, to get all of the nodes that uid_1 has rated
queryPhotos() {
photoCollection.whereField("uid_1", isGreaterThan: -1).getDocuments() { querySnapshot, error in
//the snapshot will contain photo_0 and photo_2
for doc in querySnapshot.documents { //iterate over each photo
//you'll have all of the fields here and can calculate an average rating
print(doc.documentID, doc.data())
}
}
}
why not just :
list item :[
userB :{...}
userC : {...}
]
userB (and userC) will use the structure you post below, photo1 and photo2 for each item seems redundant

How to insert multiple data in firebase using loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way to insert multiple data in firebase using loop in android studio.
As
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String Name = ds.child("Name").getValue().toString();
list.add(Name);
}
DataSnapshot loop is used to get the multiple children. How can i insert multiple data using loop.
I am guessing you want to update various children of your database at once and for that you do not need to actually use a loop to insert multiple data at once in Firebase.
You may use maps for that, which can update multiple fields of your database in one go. The code for using maps, looks something like this:
Map<String,Object> taskMap = new HashMap<>();
taskMap.put("age", "12");
taskMap.put("gender", "male");
taskMap.put("name", "Someone");
taskMap.put("surname", "no-one");
reference.updateChildren(taskMap);
This depends a lot on your database structure, and you can edit it for your need, according to your database structure.
To know more about, how to update database using maps, go through the following links:
Link1
and
Link2.
Also, if you could make the question a bit more precise, you can get more help from here.

Firebase data flattening

I have a quick question about the best practices for data structure in a firebase database.
I want users of my app to be able to maintain a friends list. The firebase documentation recommends creating a schema (not sure if thats the proper word in this context) that is as flat as possible. Because of this I thought it would be a good idea to separate the friends section from the player section in the database like so:
{
"players":{
"player1id":{
"username":"john",...
},
"player2id": ...,
"player3id": ...
}
"friends": {
"player1id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
"player2id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
}
So my questions are as follows:
Is this a good design for my schema?
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Is this a good design for my schema?
You're already thinking in the right direction. However the "friends" node can be simplified to:
"friends": {
"player1id": {
"friend1Id":true,
"friend2Id":true
}
}
Remember that Firebase node names cannot use the character dot (.). So if your IDs are integer such as 1, 2, and 3 everything is OK, but if the IDs are username be careful (for example "super123" is OK but "super.duper" is not)
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
No. If you pull /friends/1 it obviously won't pull /friends/2 etc.
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Loop through the IDs and fetch the respective nodes from Firebase again. For example if user 1 has friends 2, 3, and 4, then using a for loop fetch /players/2, /players/3, and /players/4
Since firebase pull works asynchronously, you might need to use a counter or some other mechanism so that when the last data is pulled you can continue running the completion code.

Categories

Resources