【问题标题】:IntentService onHandleIntent is running on main threadIntentService onHandleIntent 正在主线程上运行
【发布时间】:2020-10-18 17:21:33
【问题描述】:

我决定将 Intent 服务用于涉及循环大量结果、计算摘要结果并将其插入 Firestore 的任务。 这就是我从活动中调用我的意图服务的方式。

onDataSnapShotListenerForPointsAndRating = db.collection("PointsAndRating")
    .addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
            @Nullable FirebaseFirestoreException e) {
            Intent serviceIntent = new Intent(PlayWithMrMathActivitySinglePlayer.this,
                MyIntentService.class);
            serviceIntent.putExtra("SENDER", "Mr. Math");
            serviceIntent.putExtra("emailAddress", emailAddress);
            serviceIntent.putExtra("OwnID", OwnID);
            Log.d(TAG, "Hello MyIntentService:  I am Mr Math and i am starting service"); 
            **startService(serviceIntent);**
        }
    });

这就是我做大循环工作的方式:

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent: inside onHandleIntent ");
    String sender = intent.getStringExtra("SENDER");
    emailAddress = intent.getStringExtra("emailAddress");
    OwnID = intent.getStringExtra("OwnID");
    Log.d(TAG, "onHandleIntent: inside onHandleIntent sender = " + sender);
    Log.d(TAG, "onHandleIntent: inside onHandleIntent emailAddress = " + emailAddress);
    Log.d(TAG, "onHandleIntent: inside onHandleIntent OwnID = " + OwnID);
    Log.d(TAG, "onDataSnapShotListenerForPointsAndRating: ");
    Log.d(TAG, "onHandleIntent: currentThread = " + Thread.currentThread().getId());
    db.collection("PointsAndRating")
        .orderBy("gmq", Query.Direction.DESCENDING)
        .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot>task) {
                Long myWorldRank = new Long(0);
                Log.d(TAG, "onComplete: onDataSnapShotListenerForPointsAndRating =  " + task.getResult().size());
                if (task.isSuccessful()) {
                    Log.d(TAG, "onHandleIntent InonComplete : If currentThread = " + Thread.currentThread().getId());
                    for (QueryDocumentSnapshot document: task.getResult()) {
                        Log.d(TAG, document.getId() + "= onDataSnapShotListenerForPointsAndRating =@@ => " + document.getData());
                        myWorldRank = myWorldRank + 1;
                        if (document.getId().equals(emailAddress)) {
                            break;
                        } 
                        ***Log.d(TAG, "onHandleIntent InonComplete : For Loop currentThread =  " + Thread.currentThread().getId());***
                    } // end of for loop
                    Log.d(TAG, "onDataSnapShotListenerForPointsAndRating: Rank = " + myWorldRank);
                }
                final Map<String, Object> myWorldRankUpdateNugget = new HashMap<>();
                myWorldRankUpdateNugget.put("myWorldRank", myWorldRank);
                db.collection("PointsAndRating")
                    .whereEqualTo("userID", OwnID)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                for (QueryDocumentSnapshot document: task.getResult()) {
                                    db.collection("PointsAndRating")
                                        .document(document.getId())
                                        .set(myWorldRankUpdateNugget, SetOptions.merge())
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                Log.d(TAG, "onDataSnapShotListenerForPointsAndRating: WORLD RANK UPDATED ");
                                            }
                                        });
                                } // end of for loop
                            }

                            Log.d(TAG, "onHandleIntent: EXIT currentThread = " + Thread.currentThread().getId());
                        }
                    });
            }
        });

} // end of onhandle

我在日志中看到了这一点。

06-28 17:48:34.068 11076-11154/com.udiversity.myapplication D/MyIntentService: onHandleIntent: currentThread = 3867
06-28 17:49:36.118 11076-11076/com.udiversity.myapplication D/PlayWithMrMathActivitySinglePlayer: onCreate:  currentThread = 1
06-28 17:49:36.118 11076-11076/com.udiversity.myapplication D/PlayWithMrMathActivitySinglePlayer: OnSubmitPOC: currentThread Back Home OnCreate 
06-28 17:49:36.118 11076-11106/com.udiversity.myapplication D/PlayWithMrMathActivitySinglePlayer: doInBackground: currentThread = 3837
06-28 17:49:36.148 11076-11076/com.udiversity.myapplication D/PlayWithMrMathActivitySinglePlayer: onProgressUpdate currentThread = 1
06-28 17:49:36.158 11076-11076/com.udiversity.myapplication D/PlayWithMrMathActivitySinglePlayer: onProgressUpdate EXIT 1 currentThread = 1
06-28 17:49:48.328 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : If currentThread = 1
06-28 17:49:48.328 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.328 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.328 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.328 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1
06-28 17:49:48.338 11076-11076/com.udiversity.myapplication D/MyIntentService: onHandleIntent InonComplete : For Loop currentThread =  1

为什么 onHandleIntent->db.Collection("").get().onComplete() 中的这个循环在主线程上运行?

public class MyIntentService extends IntentService {

    private static final String TAG = "MyIntentService";
    FirebaseFirestore db = FirebaseFirestore.getInstance();

db 是我的本地类数据库实例。

【问题讨论】:

  • FireStore db.Collection.get.onComplete 始终在主线程上运行。无论你把它放在单独的线程或服务中。

标签: android firebase google-cloud-firestore intentservice


【解决方案1】:

onHandleIntent 的开头使用Thread.currentThread().getId() 来获取线程的ID。它被记录为 3867。在 onComplete 方法中,您再次记录线程 ID 并得到 1。您还记录嵌套 onComplete 的线程 ID,但它看起来不像已添加到提供的日志。

应该不同的线程的线程ID不同,因此代码是异步执行的。我不确定你看到了什么错误,但如果你担心的是 ID 号是 1,根据the documentation,线程的 ID 仅在线程终止的生命周期内是唯一的,它可能会再次被重用.

实际上在提供的日志中,我们可以看到线程 ID 1 正在被 PlayWithMrMathActivitySinglePlayer 使用,但在被 MyIntentService 使用之前就被终止了。

除此之外,我没有看到任何奇怪的地方。是什么让您认为代码或其执行存在错误?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多