android - Boolean not keeping most recent value of Sharedpreference result -
my boolean variable not keeping current value of sharedprefs. in functions returns true of success:
// method async. call public boolean downloadmatchesasync(string date) { brawtaapiadapter.rungetmatches(date, new callback<jsonkeys>() { @override public void success(jsonkeys jsonkeys, response response) { success = jsonkeys.issuccess(); message = jsonkeys.geterrormessage(); if (!message.isempty()) { toast.maketext(myapplication.getappcontext(), message, toast.length_short).show(); } gson gson = new gson(); string jsonobject = gson.tojson(jsonkeys); //converts java object json string' downloaded = preferences.savetopreferences(activity, applicationconstants.match_data, jsonobject); log.d(applicationconstants.tag,"in networkoperation " + downloaded); //is true @ point } @override public void failure(retrofiterror error) { } }); // call async api return downloaded; }
however when call function in method in same class returns false here:
// method async. call public void authenticateuserasync(string email, string password) { brawtaapiadapter.runuserauthentication(email, password, new callback<jsonkeys>() { @override public void success(jsonkeys jsonkeys, response response) { success = jsonkeys.issuccess(); message = jsonkeys.getmessage(); if (success) { boolean b = downloadmatchesasync(date); log.d(applicationconstants.tag, "in authenticateuserasync " + b); //is false @ point if(b){ intent = new intent(activity, matchselect.class); activity.startactivity(i); activity.finish(); } } } @override public void failure(retrofiterror error) { toast.maketext(myapplication.getappcontext(), "incorrect credentials", toast.length_short).show(); } }); // call async api }
this definition of downloaded variable:
public class networkoperations { private brawtaapiadapter brawtaapiadapter; // rest adapter private boolean success; private string message; private activity activity; private string data; private string date; private boolean downloaded = false;
and definition of preference class:
public class preferences { private static final string prefname = "net.brawtasports.brawtasportsgps"; public static boolean savetopreferences(context context, string key, string value) { sharedpreferences sharedpreferences = context.getsharedpreferences(prefname, context.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring(key, value); return editor.commit(); } public static string readfrompreferences(context context, string key, string defaultvalue) { sharedpreferences sharedpreferences = context.getsharedpreferences(prefname, context.mode_private); return sharedpreferences.getstring(key, defaultvalue); } }
i figure problem regarding scope of downloaded
. how can solve this?
i think point here is:
in downloadmatchesasync, variable downloaded changed in async method.
in authenticateuserasync, use downloadmatchesasync normal sync method (the value 'downloaded' returned before work in downloadmatchesasync.success completed, see log 'in authenticateuserasync ' appear before 'in networkoperation ')
think changed below may solve problem:
private boolean downloaded = false; private hashmap<string, boolean> authenticateusers = new hashmap<string, boolean>(); public boolean downloadmatchesasync(string date, final string email) { brawtaapiadapter.rungetmatches(date, new callback<jsonkeys>() { @override public void success(jsonkeys jsonkeys, response response) { success = jsonkeys.issuccess(); message = jsonkeys.geterrormessage(); if (!message.isempty()) { toast.maketext(myapplication.getappcontext(), message, toast.length_short).show(); } gson gson = new gson(); string jsonobject = gson.tojson(jsonkeys); //converts java object json string' downloaded = preferences.savetopreferences(activity, applicationconstants.match_data, jsonobject); log.d(applicationconstants.tag,"in networkoperation " + downloaded); //is true @ point // if downloaded , user authenticated, start activity if (downloaded) { boolean isauthenticated = authenticateusers.get(email); if (isauthenticated != null && isauthenticated) { intent = new intent(activity, matchselect.class); activity.startactivity(i); activity.finish(); } } } @override public void failure(retrofiterror error) { } }); // call async api return downloaded; } public void authenticateuserasync(final string email, string password) { brawtaapiadapter.runuserauthentication(email, password, new callback<jsonkeys>() { @override public void success(jsonkeys jsonkeys, response response) { success = jsonkeys.issuccess(); message = jsonkeys.getmessage(); // store authenticate result authenticateusers.put(email, success); if (success) { // boolean b = downloadmatchesasync(date); // check if work in downloadmatchesasync completed; boolean b = downloaded; log.d(applicationconstants.tag, "in authenticateuserasync " + b); //is false @ point if(b){ intent = new intent(activity, matchselect.class); activity.startactivity(i); activity.finish(); } else { downloadmatchesasync(date, email); } } } @override public void failure(retrofiterror error) { // store authenticate result authenticateusers.put(email, false); toast.maketext(myapplication.getappcontext(), "incorrect credentials", toast.length_short).show(); } }); // call async api
}
hope help
Comments
Post a Comment