setValue onComplete not called from Firebase on Android -
i'm using security rules determine in conditions value can written database. @ moment i've setup security rules, these working fine (i checked simulator), no issues far.
in case permission write denied (security rules not met), want take action. in order this, i'm planning use oncomplete on setvalue , if there error have code.
at moment, if security rules met, values written online database , can check in browser. if value not written database due security reasons, can see error in debug/log saying permission denied, good.
the issue when value written or not written database, oncomplete not called @ all, it's doesn't exist in code. checked in debug mode breakpoints. code i'm using comes straight documentation, please see below.
does have ideas? lot!
dataref.setvalue(frequency, new databasereference.completionlistener() { @override public void oncomplete(databaseerror databaseerror, databasereference dataref) { if (databaseerror != null) { system.out.println("data not saved " + databaseerror.getmessage()); } else { system.out.println("data saved successfully."); } } });
edit
i noticed above behavior if setvalue method called within addvalueeventlistener/ondatachange. if move setvalue method outside listener, oncomplete gets called. expected behavior or bug?
update
after further testing, noticed doesn't matter if setvalue method called within addvalueeventlistener/ondatachange. matters though if call method within while loop below. long within while loop, oncomplete not called. in both cases values written/not written database depending on security rules. please see both cases below:
option 1 - oncomplete not called
firebasedatabase.getinstance().setloglevel(logger.level.debug); firebasedatabase database = firebasedatabase.getinstance(); final databasereference databasereference = database.getreference(); databasereference dataref = databasereference.child("drivers").child("23").child("frequency"); while (errorstatus == false) { dataref.setvalue(frequency, new databasereference.completionlistener() { @override public void oncomplete(databaseerror databaseerror, databasereference databasereference) { if (databaseerror != null) { system.out.println("data not saved " + databaseerror.getmessage()); errorstatus = false; frequency = frequency + 1; } else { system.out.println("data saved successfully."); errorstatus = true; frequency = 1; } } }); }
option 2 - oncomplete called.
firebasedatabase.getinstance().setloglevel(logger.level.debug); firebasedatabase database = firebasedatabase.getinstance(); final databasereference databasereference = database.getreference(); databasereference dataref = databasereference.child("drivers").child("23").child("frequency"); dataref.setvalue(frequency, new databasereference.completionlistener() { @override public void oncomplete(databaseerror databaseerror, databasereference databasereference) { if (databaseerror != null) { system.out.println("data not saved " + databaseerror.getmessage()); errorstatus = false; frequency = frequency + 1; } else { system.out.println("data saved successfully."); errorstatus = true; frequency = 1; } } });
i need while loop though, unless workaround can found?
update
public class mainactivity extends appcompatactivity { static boolean errorstatus = false; static int frequency = 1;
Comments
Post a Comment