0

Let's say I have this Function that sets a value in Firebase Database:

    public  void RegisterUserInDB(FirebaseUser user,Profile p,Activity activity){

        DatabaseReference ref= database.getReference("Users");
        ref.child(user.getUid()).setValue(p).addOnCompleteListener(
                new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(
                                    activity.getApplicationContext()
                                    ,"DB Updated"
                                    ,Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
                }
        );
        dbProfile=p;
        localProfile=p;
    }

I want to use that function somewhere else like this:

RegisterUserInDB(user,p,activity);
doSomeThingAfter();

but I want everything to wait till that task inside the function finishes, I understand that I'm only adding a listener here but not running the task, but as mentioned earlier, something depends on the code I'll put in the onComplete method. Can someone help :)?

Also, do you think it's a bad use to pass the Activity as a parameter?

Every time I do that, the function doSomeThingAfter() executes before the task is done.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Mesameh
  • 23
  • 6
  • Try using [wait](https://www.baeldung.com/java-wait-notify#2-waitlong-timeout). Also its not necessary to pass the activity as a parameter because you can call `this.getApplicationContext`. – Alias Cartellano Mar 23 '23 at 17:29
  • 1
    Aside from my answer, if you understand Kotlin, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5) will help. Here is the corresponding [repo](https://github.com/alexmamo/RealtimeDatabase). – Alex Mamo Mar 24 '23 at 06:32

1 Answers1

1

Since you're attaching an OnCompleteListener to the setValue() operation, it means that there will always be a guarantee that you'll get either a completion result or an Exception, but you'll have to check that. How can you do that? Simply, by calling isSuccessful() method on the Task object. So if the Task is successful it means that the operation for adding the data to the Realtime Database is complete and there were no errors. On the other hand, if the Task isn't successful, then an Exception is thrown. Such an Exception usually arises when you have un proper security rules. And remember, it's always one, or the other, never both.

Since all Firebase operations are asynchronous, the solution is always the same. If you need to perform some action only when you want to be 100% sure that the operation for adding data to the database is complete, then any code needs to be inside the onComplete method or be called from there. So in your case, right after the Toast.

If you need to use the result of an async operation outside the onComplete, then I recommend you check my answer from the following post:

It's true that the answer refers to a read operation, but the exact same rules apply to the setValue operation.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Mesameh. Did my answer help? Can I help you with other information? – Alex Mamo Mar 25 '23 at 12:55
  • 1
    Yes, I now understand how it works and have already implemented that in my code. Thanks a lot. – Mesameh Mar 30 '23 at 09:13
  • Also, I don't use Stackoverflow much, so I wanted to ask you another question unrelated to this topic, but I don't know where to ask you. – Mesameh Mar 30 '23 at 09:15
  • 1
    I'm only available, here, on StackOverflow. – Alex Mamo Mar 30 '23 at 09:28
  • I appreciate your help; I have a question; I'm used to Java and building an android app now with it; I would like to have an IOS version in the future; which language should I use for the IOS app? – Mesameh Mar 31 '23 at 10:05
  • @Mesameh I will recommend you [Swift](https://developer.apple.com/swift/). – Alex Mamo Mar 31 '23 at 11:21