Firebase user data storage issue - java

I am working with Firebase Authentication and while coding I realised that I can use User user = FirebaseAuth.getInstance().getCurrentUser; to fetch user data in any activity through out the application if user successfully logged in but I am confused that whether I can store user data in sharedPreferences after user login or I can use User user = FirebaseAuth.getInstance().getCurrentUser; way to get user data in whichever activity I want to. So which method is preferable to use firebase user data. And if there any other method you have to suggest then you are always welcome. Thanks in advance.

The user data is limit to login info like user id or email address by using FirebaseAuth.getInstance().getCurrentUser().
If it's enough to use it for you, that's ok.
In order to get the data which saved to sharedpreferences, you can define PreferencesUtils class.
The class is singleton or has static members.
If so, I think you can call this members anywhere.
I hope it helps.. If you any questions, let me know.

Use always firebase user instance, because it gives to you an updated user instance. if you are going to save user data in sharedPreferences, you should always save user details in sharedPreferences when you update the user data.

I would use an AuthStateListener to listen to possible changes to the authenticated user, and persist changes to that user object only when they might change, according to that listener. It's probably not worthwhile to continually ping currentUser for changes when you know it can only change when response to the listener callback.

Related

How to bind firebase database user and user data

I don't really know what to search regarding this specific question but recently I just did
a login/sign up feature on my app with firebase and it works great.
Context of what I wanna do:
This app already has a working assessment feature that will give a score to the user and will store the score in their profile.
The problem is how do I make sure that the score (and other user info) will be bound to a specific user that is currently logged in?
How do I make it so that when a specific user does an assessment their score will fall into their own node?
example of what I want to accomplish:
>USER1 signs up.
>USER1 will take the assessment.
>After USER1 takes an assessment
he will go to the profile to see his results
as well as his basic info like fullname,username and age.
This is the sample of my firebase database.
Thank you! I will keep searching I just need a little bit of help cause I don't really know what to search about this specific thing.
If the user is logged in, then you can simply do:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
Now you have access to the userId. Then if you want to add a new score to the database, you can do:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Score");
ref.child(userId).child("result").setValue(1);
This will give you the following database:
Score
userId
result : 1

How to best store IDs of other database objects within Firebase (Ex: Event with Users)

I am new to firebase, and am building an android app to practice.
My app is very straightforward. There are Users, who can create an Event. Other users can either accept or reject an invitation to an Event.
To keep things simple, whenever an Event is created, all Users in the database get invited to that Event.
My database is currently structured like this:
What I want to do now is for each Event, I want to know which Users have accepted, declined, or are yet to respond to the invitation.
I am unsure the best way to approach this. These are some ideas I have had:
For an Event, create 3 children which will store all the User information for each response option. (accepted, rejected, pending).
For an Event, create a child 'responses' which will have each Users uid as a key, and the value could be either "accepted", "rejected", or "pending".
Something else completely.
Option 1 seems like it can work, however it means that a User data is going to be copied all over the database. If a User is invited to 1000 events, there will be a copy of that User literally 1000 times, once within every Event.
Option 2 seems logical to me, however I have tried implementing it and am having difficulties building a firebase Query. If I want all the accepted users, I need to get all the uids of the 'acceptedUsers' child, and then pull out of the 'users' node all the children that match these uids. This doesn't seem to be possible...?
Any help or guidance would be greatly appreciated!!
Option 2 seems most direct to me:
responses
eventid
uid1: "accepted"
uid2: "rejected"
uid3: "pending"
You will indeed have to load the additional information for each user client-side. This works fine, and performs much better than most developers expect due to the fact that the requests can be pipelined over a single connection. For more on this see my answer here: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786
If you'd really prefer not to perform the client-side join, you can store additional data for each response that allows you to display it. If you want to display user names:
responses
eventid
uid1
status: "accepted"
displayName: "Ryan Saffar"
This gets closer to Peter's answer, but here we key them off event id and uid.
Try this:
Events
eventid1
createdby: userx
eventname: Party
eventid2
createdby: usery
eventname: Meetup
AttendancetoEvent
eventid1
username: userf
response: accepted
eventid1
username: userg
response: rejected
eventid2
username: userg
response: accepted
then to get the accepted users, do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("AttendancetoEvent");
Query queries=ref.child("eventid1").orderByChild("username").equalTo(accepted);
Well you're doing everything right, just to say. For the Events node, you can use .push() function for random branch name. So let's begin, each User has a UID, alright? User's data is particularly stored there including their name, that's done. Now a user posts an event so push that event to Events, now make an arrayList of user's event in the UID node. So when you upload that event you get it's reference and store it in the UID branch so the user can retrieve all of his events.
For accepted, pending or rejected choices , I hope whatever you're building has buttons for these functions.
So now onClickListener() store the user's
Uid in an arrayList for any of the choices and upload it to the branch of retrieved Event. For that in your Event model class add a String for the path reference, so you can upload the choices there. All done.
Now if the user who wants to know who accepted, rejected or have pending choices you can display the user name using UID from the arrayList. I suggest to use uid because just in case if the event uploader wants to get any more of the User data who chose any of the options.
I think this will work fine.

How to maintain state of object throughout the app and change the UI at the same time

I'm developing an app having HomeFragment which displays posts of the user you are following. Now each post is having Like and Comment option and when you click on user profile pic or name, it will redirect you to his/her profile which again displays all his/her posts. Now again if you click on one of the post it will redirect you to post detail screen which implements ViewPager where you can change the post by swapping left or right.
My question is every post has Like and Comment option with Like and Comment count, if you like any one post then it should reflect at all the activities(Home, User profile and post detail).
Currently I'm handling all these using startActivityForResult and onActivityResult (eg: if you like post at position 2 and when you click back button then I'm sending that position with new count and notifying that particular position with new like count at HomeFragment) which is very confusing and I think is not the proper solution.
Is there any Design Pattern or any other mechanism to handle this?
If you didn't get my question then let me know, I'll elaborate it more.
Thank you.
My approach in my apps:
In activity onCreate I start Intent service to load data from server. In Intent service data are loaded from server, then parsed and pushed to local database (I use super fast Realm database). When intent service finished, UI is notified to refresh it's content. When you return to previous activity onResume is called where I reload data from database and refresh UI.
To sum it up, UI works only with local database where all data are stored. When new data are loaded on background thread, this data are also pushed to local database. So no need to send loaded data (only needed object ID's) between activities because database contains always up to date data.
You can create singltone class which will hold every posts likes and comments.
Every time you open activity/fragment you pull data from this class.
Every time user adds like or comment you push data to this class.
I also recomend you to take a look at MVP pattern

OIM 11gR2 PS3 Save Justification of Request on Create User

In OIM 11gR2 PS3, When creating a user I want to save the String entered in the Justification box of the request. When creating the user I have no workflow set, because I don't need any approvals. Do you know any way to do this??
Thank you
I found out that the best way to retrieve the justification of the request on create user is not through the API, but querying the DB and getting the justification field from there, given the login of the user. I also ordered the results by date, in order to retrieve the most recent request.

Check if user Name exist

What i want todo with Java and Javascript:
If a user try to register an Account, after he write the Login name and klick in the next field, there should be an immediately check, if the Login Name already exist.
My Question is now, what is the best performance way.
I know, how i can check the username in the database, that is no Problem.
But is it possible to cache the List of users in a Application wide variable ?
If yes, how or where should i create a such variable ? I use tomcat as server.
But no idea how i can do that.
Or is it just fine, todo a check on the DB Server.
I want something similar like the Registration from hotmail
Thanks
Two ways to do it (and not thinking very hard). First - before loading the page, on the server side retrieve all user names, put the in a list and put the list in the request. Now you have all your user in the page and can check if the entered name exists (must do it in javascript). The second method - after typing the name make an ajax call to the server and check in DB if exists. Hope this helps.
It's not a good idea to cache all login names. First because at every http session, you need to refresh the whole cache. Second because it's possible to have multiple http sessions (multiple user trying to create an account) and you need to refresh the whole cache to verify new registrations login names. Third it's not a good practice to store temporary the whole user names table in such a variables.. imagine you have 10000000 login names!
With a cache, if two users want to register at the same time, and enters the same user login, both user login pass the validation!
Just query your database with an ajax request or a servlet and make sure your login name column has an index!

Categories

Resources