Search code examples
javaandroidarraysfirebasefirebase-cloud-messaging

How can I wait until all FCM subscriptions are completed


I fetch the server and get an array with topics to subscribe to, I loop it and subscribe to each topic, What I want to do is wait for this to complete then start new activity .

I did it with number of loop , If it equals the length minus one of array then start new activity . Here is my code :

int length = subscribeTo.length();
for (int i = 0; i < length; i++) {
    String sub = null;
    try {
        sub = subscribeTo.get(i).toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    int finalI = i;
    JSONObject finalData = data;
    String finalSub = sub;
    FirebaseMessaging.getInstance().subscribeToTopic(sub).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Log.e("sub", finalSub);
            if (!task.isSuccessful()) {
                Toast.makeText(MainActivity.this, "can't login in", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.e("test", "sub" + finalI);
            if (finalI == length - 1) {
                try {
                    editor.putInt("sub_length", length);
                    editor.putString("restaurant_name", finalData.get("restaurant_name").toString());
                    editor.putString("username", finalData.get("username").toString());
                    editor.putString("password", finalData.get("password").toString());
                    editor.putString("sub" + finalI, finalSub);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
                editor.apply();
                finish();
            }
        }

    });
}

There is no better way to do ? I'm new to java by the way .


Solution

  • No one seems to have responded Whatever it is, I searched a lot and here is how I solved it Using CountDownLatch

    The code :

    int length = subscribeTo.length();
    CountDownLatch latch = new CountDownLatch(length);
     
    for (int i = 0; i < length; i++) {
        String sub = null;
        try {
            sub = subscribeTo.get(i).toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        int finalI = i;
        String finalSub = sub;
        FirebaseMessaging.getInstance().subscribeToTopic(subscribeTo.get(i).toString()).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                editor.putString("sub" + finalI, finalSub);
                latch.countDown(); // The number will decrease
            }
        });
    }
     
    latch.await(); // Here it will wait until it ensures that all subscriptions are completed
    editor.putInt("sub_length", length);
    editor.putString("restaurant_name", dataa.get("restaurant_name").toString());
    editor.putString("username", dataa.get("username").toString());
    editor.putString("password", dataa.get("password").toString());
    editor.apply();
    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
    startActivity(intent);
    Log.e("done", "yes");