I\'ve created a method to take a users facebook data, after they login, and create a \"user\" for them on my firebase database. this method, addUser(), also creates and sets
You need to set a preference like FIRST_LAUNCH
and check if its true each time your user logs in. First time the application launches, the FIRST_LAUNCH
preference won't be found. So call your addUser()
function to create a new entry in your FireBase database.
SharedPreferences pref = getSharedPreferences(Constants.ApplicationTag, Activity.MODE_PRIVATE);
if (!pref.contains(Constants.FIRST_LAUNCH)) {
addUser();
pref.edit().putBoolean(Constants.FIRST_LAUNCH, true).commit();
}
So you might be thinking of if an user uninstalls your application and then reinstalls it, the preferences will be gone and the addUser()
function will be called again. No problem, you won't get a new Firebase entry as long as the path to the child attribute is the same. The values will be replaced to the specific path (if it does exist), with current information of user.
Now if you want to check if your user already exists in Firebase database you need to add a listener like this. I'm attaching a code sample for better understanding.
Firebase rootRef = new Firebase("https://<url>.firebaseio.com/users/");
Firebase userRef = rootRef.child(mAuthData.getUid() + "/");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// User exists. Do nothing
} else addUser();
}
@Override
public void onCancelled(FirebaseError firebaseError) {}
});