I am working on an Android application where there are two types of user. I differentiate them into the system by using the field UserType. I have a requirement where I have to copy data from one child node into another. I am using Firebase for my backend. I have gone through other answers but this requirement is unique in a way that, I want to copy data only on a particular node. Attaching the Firebase database structure
In this structure, if you can see I have expanded 2 child nodes of "users". One of the child has userType as Standard User and other child has userType as Guardian User.
1) I want to copy the "fullNameGuardian" child alone from the user with key starting with "L4bTr6q" to the user with key starting with "dC9Mq"
2) I want to copy the "standardEmail" child alone from the user with key starting with "dC9Mq" to user with key starting with "L4bTr6q".
And everytime I add a new user, the user can be with one of the userType "Standard User" or "Guardian User". So is there a possibility like I do it for every new user?
I am having difficulties figuring out how to do this. Any help is appreciated. Thanks a lot in advance.
I think you could save the other structure when you save the first one. For instance,
Save - node1/child
Update - node2/child
//and so on
Another way is using Firebase functions(Real database triggers) in order to automatically do something after something else had happened. Check it out.
Related
I want to delete the yellow user in the screenshot if he decides to delete his account. The blue X is a different user. How do I do that?
In the second picture is my attempt. I don't know how to get the userid of the blue X.
Your current data structure makes it easy to find the yellow Followers for a given blue Follow node. It does not however make it easy to find the blue node for a given yellow one. In fact, to allow that you will have to read all blue nodes, and check each individual Followers child node in turn.
To more easily allow your use-case, you'll want to also store the inverse data structure that allows you to find the blue Follow nodes for a given yellow Follower value. I typically refer to this as the Followee map, although I admit I'm not sure if that's even a word:
Followees: {
"ky....Yhi1": {
"L5w...6rF3": true
}
}
Now with this structure, you can find the "L5w...6rF3" key based on knowing the "ky....Yhi1" value and delete the user from the Followers node there.
We covered this scenario a few times before, so I recommend also checking out:
Firebase query if child of child contains a value
Firebase Query Double Nested
Many to Many relationship in Firebase
I can successfully retrieve documents from firestore, the trouble is, i do not know how to put them back where they came from!!
My app is a court booking system, it uses 7 fragments that represent the days of the week. Each fragment contains buttons that represent booking slots throughout the day. When a button is pressed, the court booking activity fires showing textviews and spinners.
The information I save to firestore includes a unique number representing a datestamp and a booking id that represents the id of the button that was pressed.... from here, I am lost, i need to write the retrieved database info back to their relevant places but i dont have anything unique in the way of widgets. The buttons are unique but all they do is fire a non unique court booking activity... any help appreciated... sorry for length, quite possibly more to add when answering questions.
To write to a document in Firestore you need to know the complete path to that document. You'll need to track the necessary IDs in your code, in a way that you can synchronize it with your UI elements.
A simple first pass could be to add a non-editable view (e.g. a label) to your UI for each document, and set the document ID (or entire path) in there. Then when the user clicks a button, you find the corresponding view with the ID, and from that can recreate the DocumentReference that you need to update.
I have (somewhat) large list of jokes in my Firebase Database like in the image below.
I display them in a list in my Android app something like a feed. I also implemented possibility to log in with Firebase Authentication and now I want to add options for logged users to (dis)like jokes and add them to favorites (favorites are supposed to be like private bookmarks). I'm wandering how I could structure my data and I have two proposals:
Add new root node called "userJokes" with child nodes representing user UID from Firebase Authentication. Every UID child node should have copy of every joke from "joke" node with additional booleans representing (dis)like and favorite states.
Another solution is to add every user UID to a joke a user (dis)likes or adds to favorite.
First solution is logical, but how could I count number of likes and dislikes if I structure data this way? And what is the best way to copy every joke from "joke" node to "userJokes" node for every user to be showed in the feed? Second is impractical since while retrieving jokes, I will get info about every user that has (dis)liked or added to favorites and this is not what I need. Which solution is better? Is there any other? Is it OK to add user UID from Firebase Authentication to database in Firebase Database?
I think the first one is more accepted, although it needs some tweak :)
First note: if you create data only to be used as relational (like userJokes), it's better to just add simple value to it without copying entire source data (jokes data), like this:
userJokes: {
randomUserId: {
randomJokeId:true,
anotherRandomJokeId:true
}
awesomeUser: {
randomJokeId:true
}
}
Second note: if you want to implement two functionality (like and favorite), I think you should make it as different data. So it would be userJokeLike and userJokeFavorite (or something like that). And the structure for each of them should be same as I mentioned in first note.
In conclusion:
Every joke data is still in their source path (i.e. inside jokes) and ONLY their id is copied into newly created data path (userJokeLike and userJokeFavorite)
When you want to search for joke that user with id randomUserId likes, you should check for userJokeLike\randomUserId. Then from every joke id you got there, get the real data from inside source jokes path.
When you want to search for joke that is favorited by user with id randomUserId, basically, do the same as above.
When you want to count likes and favorite of each joke, just use something like this:
FirebaseDatabase.getInstance().getReference("userJokeLike")
.orderByChild().equalsTo("randomJokeId")
.addListenerForSingleValueEvent(new ValueEventListener() {
... onDataChange(DataSnapshot dataSnapshot) {
int jokeCount = dataSnapshot.getChildrenCount();
}
});
And there you go, hope this helps.
Note: I haven't check the last code, hope that work :p
EDIT:
Looks like I did misunderstand :D
The solution above is what I think is best for the sake of structure itself. But if we need something simple and fast, it is different for each case/situation. I think that the best solution if you want to get jokes with likes and favorites included (no need to create another request) then your structure should look like this:
jokes: {
randomJokeId: {
// joke data here
likes:{
randomUserId:true,
anotherUserId:true
},
favorites:{
randomUserId:true
}
}
}
It includes likes and favorite when you request jokes data. SO in each data you only need to check if current user's UID is exist inside likes and/or favorite. And the counter will be a lot easier this way.
Happy coding :)
i have created my app, in which I have created several users. The challenge is, I need to construct the app in a way that, while I am logged in as a user (say User A), I can add input an object into another user (say User B). So, while am logged in as user A, I can input the data that will be saved into one of the empty field, of User B. Can you assist which way, I can arrange this? In short, how can I create an object to associate with the User B.
Here is the thing, while I am logged in as User A, I want to add "Scores" for both user A and B. for instance, if the user A's score is 7, and B's score is 20, I could add the scores while logged in the activity A, and they will be added in the field "Scores" of the corresponding users.
you should go for parse cloud code which is written in java script
refer this link to know more
https://parse.com/docs/js/guide#cloud-code
then you just edit main.js
deploy and call that cloud code from your code
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("updateTable", function(request, response) {
var currentUserId=request.params.currentLoggedinUser;
var userToBeUpdatedId=request.params.userToBeUpdated;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userToBeUpdatedId });
user.addUnique("followers",currentUserId);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
deploy this code using comand "parse deploy" from terminal.
call this code from your code like
ParseCloud.callFunction("updateTable", params);
i hope it will help.
The way that Parse is setup by default is so that only the current users data can be editted in the User table. You could probably change the settings for the table however, this could cause security issues.
What I would suggest would be to create a 'Scores' tables. There you could have a row for each user. with a 'userID' column which contained the objectId of the user to reference the user. You could then have a score column which you could update whenever you wanted.
Alternatively to storing the objectId of the user in the userID column you could make an association between the Scores row and the user. See the Parse documentation for how to create associations. https://parse.com/docs/android_guide#users-associations
I am trying to develop a user input form where i take his details and store them
in the form i have fields for his name, email, address, phone number etc.
a person may have multiple emails or multiple phone numbers.
he can add a field by clicking a button.
and then i want to store the data entered by the user in a shared preference.
i have a question:
how do i retrieve data from dynamically added extra fields by the user?
I have maintained a count for dynamically added fields for each field criteria (like email, phone etc)
but i am stuck at the point where i am supposed to retrieve the data when i need to store them in Shared Preferences.
Please help! thankyou in advance.
Is it possible using something like ParentViewGroup.getChildCount() or something else?
Sure, you can use getChildCount() and getChildAt() methods to retrieve child from a ViewGroup.
However, I think the better way is to save the references to the Views you added when you add them into the View hierarchy, and get data from these references.