android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Apache License 2.0
15.51k stars 3.32k forks source link

Seems Like DiskUserDataStore Calling From main Thread #261

Closed drayan85 closed 6 years ago

drayan85 commented 6 years ago

I Have added the below Line in DiskUserDataStore class

@Override
public Observable<UserEntity> userEntityDetails(final int userId) {
     System.out.println("DiskUserDataStore Thread ID : " + Thread.currentThread().getId());
     return this.userCache.get(userId);
}

It is Printing below Line:

01-25 13:45:39.470 3117-3117/com.fernanependocejas.android10.sample.presentation I/System.out: DiskUserDataStore Thread ID : 1

Is that mean that method has been executed in main thread instead of running in one of the thread which should have been created in JobExecutor Thread pool?

mjohenneken commented 6 years ago

you code is running on main Thread if

Thread.currentThread().getName().equals("main");
drayan85 commented 6 years ago

@mjohenneken yeah, I can see that method is executed in the main thread. I didn't change any code from this repository after clone. I am just wondering it suppose to run on worker thread which is created in JobExecutor Thread pool.

alexwhb commented 6 years ago

@drayan85 It's fine for that userEntityDetails() is running on the main thread. That's expected behavior. what you should be checking is if this.userCache.get(userId) is being called on the main thread, since that's actually the observable. Anything outside the observable chain will not run on your executor thread. so... if you did this:

@Override
public Observable<UserEntity> userEntityDetails(final int userId) {
     return this.userCache.get(userId).map(val->{
           System.out.println("DiskUserDataStore Thread ID : " + Thread.currentThread().getId());
           return val;
         });
}

That should be on the executor thread, because it's part of the observable chain. I hope that makes sense. And do note some operations do not run on the executor thread by default, like doOnNext() and others definitely check out the docs or test before making an assumption of what thread an operation executes on. That has bitten me several times.

drayan85 commented 6 years ago

@alexwhb Thanks. I got the point.