Android - Firebase; on signing in, retrieve data (layered) and redirect that user based on that data -
so have simple firebase android application, i'm aiming cater number of different users. user registers, , on creation, uid stored in user table, along userinformation entry under uid values 'usertype' , 'iscurrentlytracking'. user signs in, brought profile activity can press button continue on through app. here table structure:
on pressing continue, userinformation data retrieved. based on data, redirected activity. instead of doing aforementioned, application stops/crashes.
here code/method redirects user:
public void userredirect(){ firebaseauth = firebaseauth.getinstance(); user = firebaseauth.getcurrentuser(); final firebasedatabase database = firebasedatabase.getinstance(); query q = database.getreference("users/" + user.getuid() + "/userinformation"); q.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot postsnapshot: datasnapshot.getchildren()) { userinformation ui = datasnapshot.getvalue(userinformation.class); int usertype = ui.getusertype(); boolean ict = ui.gettrackingstatus(); if ((usertype == 1) && (!ict)) { finish(); startactivity(new intent(profileactivity.this, allocationactivity.class)); } } } @override public void oncancelled(databaseerror databaseerror) { databaseerror.getmessage(); } }); }
i have been able retrieve data before, although information not stored in 'users' table (only under user's uid), nor did userinformation descend uid.
for context, here user class:
public int usertype; public boolean iscurrentlytracking; public userinformation(int usertype, boolean iscurrentlytracking){ this.usertype = usertype; this.iscurrentlytracking = iscurrentlytracking; } public int getusertype() { return usertype; } public boolean gettrackingstatus() { return iscurrentlytracking; }
any ideas on might going wrong? thank in advance.
i suggest use firebaseui authentication , reference uid. suggest don't use "+" concatenate firebase paths. try instead:
private databasereference databasereference; ... databasereference databasereference = firebasedatabase.getinstance().getreference().child(firebaseauth .getinstance().getcurrentuser().getuid()).getref();
and add listener this:
listener = new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot snapshot : datasnapshot.getchildren()) { // handle userinformation here } } @override public void oncancelled(databaseerror databaseerror) { } }; databasereference.addvalueeventlistener(listener);
the reason prefer separating instead of overriding inside method can following in ondestroy:
@override protected void ondestroy() { super.ondestroy(); databasereference.removeeventlistener(listener); }
keeping databasereference , listener member variables makes easier.
also, sure set appropriate firebase rules hardening users access data:
{ "rules": { "$uid": { ".write": "$uid === auth.uid", ".read": "$uid === auth.uid", } } }
for more complete example, check out todo list app made uses same database organization pattern here. hope able help, feel free comment if have other questions or if unclear.
Comments
Post a Comment