I have this: } else if (!(this instanceof LeaderboardActivity) && id ==…
- User
I have this: } else if (!(this instanceof LeaderboardActivity) && id == R.id.leaderboard) { Toast.makeText(BaseDrawerActivity.this, "Leaderboard pressed", Toast.LENGTH_SHORT).show(); MainActivity.saveToDB(false); openLeaderboardActivity();
And this public static void saveToDB(boolean Create) { Map<String, Object> userdb = new HashMap<>(); userdb.put("score", amount);
if (Create) { usersRef.document(Objects.requireNonNull(mAuth.getUid())).set(userdb) //update-set .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void unused) { Log.d("TagDB", "Successfully added"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w("TagDB","Error adding document",e); } }); } else { usersRef.document(Objects.requireNonNull(mAuth.getUid())).update(userdb) //update-set .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void unused) { Log.d("TagDB", "Successfully modified DB data!"); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w("TagDB","Error adding document",e); } }); } }How can I do that it does NOT starts creating the Leaderboard inside LeaderboardActivity onCreate until MainActivity.saveToDB(false) is finished?
- Assistant
- User
Thanks. I've used the function before inside MainActivity, like this. private void resetCoins() { amount = 0; saveToDB(false); [...] }
- Assistant
Since you want to wait for
saveToDB(false)to complete before proceeding with yourresetCoinslogic or any other operation, you would need to adjust theresetCoinsmethod to accommodate the asynchronous nature ofsaveToDB.Below is an example of how you could modify the function in to use the new callback mechanism:
- User
No, I do not want to wait here. I just want to be able to use it as before any modifications. But now it wants the 2nd parameter also. What do I do here to be able to use it like before? I don't need to wait here.
- Assistant
If you want to keep the ability to use
saveToDBwithout providing a callback (for example, when you don't need to wait for the operation to complete), you can modify the method to accept a nullable callback. This way, you can passnullwhen a callback is not needed.Here's how you might modify the
saveToDBmethod:public static void saveToDB(boolean create, @Nullable final DbOperationCompleteListener listener) { // Existing code... OnSuccessListener<Void> successListener = new OnSuccessListener<Void>() { @Override public void onSuccess(Void unused) { Log.d("TagDB", "Database operation successful"); // Check if the listener is not null, then call the callback method if (listener != null) { listener.onDbOperationComplete(); } } }; // Rest of the existing code... }Now, when calling
saveToDBfrom your method or anywhere else where you don't need to wait for the result, you can simply pass as the second argument: