简体   繁体   English

我如何才能等到所有 FCM 订阅完成

[英]How can I wait until all FCM subscriptions are done

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.顺便说一下,我是 java 的新手。

No one seems to have responded Whatever it is, I searched a lot and here is how I solved it Using CountDownLatch似乎没有人回应不管它是什么,我搜索了很多,这就是我使用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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何并行运行多个基于过滤器的 pubsub 订阅? - How can I run multiple filter based pubsub subscriptions in parallel? 我可以将 OneSignal 令牌导入 FCM 吗? - Can I import OneSignal tokens to FCM? FCM,如何让组中的所有用户订阅主题 - FCM, How to make all users in group to subscribe to topic 如何更新 FCM 令牌 - How to update FCM token BigQuery 挑战:如何根据下一个可用值正确分配值并将其保留到下一次更新? - BigQuery Challenge: How can I correctly assign the value by next available value and keep it until the next update? 如何使用 Node.js 向所有 Android 设备发送 FCM 通知 - How to send FCM notification to all android devices using Node.js 在使用 reactjs 发送 POST 请求之前,我如何等待 firebase 检查用户是否有效? - how can i wait for firebase to check the user is valid before sending a POST request with reactjs? 如何打印登录用户创建的所有文档字段 - How Can I print all docs fields that created by logged in user 如何使用 ajax 请求在 php 中实现 FCM? - How to implement FCM in php with ajax request? 如何在后台 FCM/Flutter 中保留 AndroidNotificationDetails - How keep AndroidNotificationDetails in background FCM/Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM