-1

I decided to use intent service for a task involving looping big number of results, calculating a summary result and inserting it back in firestore. This is how i am calling my intent service from activity.

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);**
        }
    });

This is how I am doing that big loop work:

@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

and i see this in logs.

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

Why this loop inside onHandleIntent->db.Collection("").get().onComplete() is running on main thread ?

public class MyIntentService extends IntentService {

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

db is my local class database instance.

Ajordat
  • 1,320
  • 2
  • 7
  • 19
hina abbasi
  • 445
  • 1
  • 4
  • 14
  • FireStore db.Collection.get.onComplete is always running on main thread. no matter u put it in separate thread or service. – hina abbasi Jun 28 '20 at 13:42

1 Answers1

0

At the beginning of onHandleIntent you use Thread.currentThread().getId() in order to get the thread's ID. It is logged as 3867. Inside the onComplete method you log again the thread ID and you get 1.You also log the thread ID of the nested onComplete, but it doesn't look like it has been added to the provided logs.

The thread IDs of the should-be-different threads are different and thus the code is being executed asynchronously. I'm unsure of what error do you see, but if what worries you is that the ID number is 1, according to the documentation the ID of a thread is unique only during its lifetime when a thread is terminated it may be reused again.

Actually in the provided logs we can see that the thread ID 1 is being used by PlayWithMrMathActivitySinglePlayer but is terminated right before being used by MyIntentService.

Apart from that I don't see anything strange. What is making you believe that there's an error on the code or its execution?

Ajordat
  • 1,320
  • 2
  • 7
  • 19