Rx Java zip completes when any subject completes - java

I am facing with the problem. As far as I know zip method from RxJava waits for all observables to complete.
But am I getting another behaviour.
Here is my code snippet
private PublishSubject<Void> firstSubject;
private PublishSubject<Void> secondSubject;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadData();
mDrawerHeaderView.postDelayed(new Runnable() {
#Override
public void run() {
// getSecondSubject().onNext(null);
}
}, 1000);
mDrawerHeaderView.postDelayed(new Runnable() {
#Override
public void run() {
getFirstSubject().onCompleted();
}
}, 1000);
}
protected PublishSubject<Void> createFirstSubject() {
firstSubject = PublishSubject.create();
return firstSubject;
}
protected PublishSubject<Void> createSecondSubject() {
secondSubject = PublishSubject.create();
return secondSubject;
}
protected PublishSubject<Void> getFirstSubject() {
return firstSubject;
}
protected PublishSubject<Void> getSecondSubject() {
return secondSubject;
}
private void loadData() {
Observable<Void> firstSubject = createFirstSubject();
Observable<Void> secondSubject = createSecondSubject();
Observable<Boolean> allDataTask = Observable.zip(firstSubject, secondSubject, new Func2<Void, Void, Boolean>() {
#Override
public Boolean call(Void aVoid, Void aVoid2) {
return true;
}
});
allDataTask
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Boolean>() {
#Override
public void onCompleted() {
Notifications.showSuccessMessage(getApplicationContext(), "COMPLETE");
}
#Override
public void onError(Throwable e) {
Notifications.showErrorMessage(getApplicationContext(), "ERROR");
}
#Override
public void onNext(Boolean aBoolean) {
Notifications.showSuccessMessage(getApplicationContext(), "NEXT");
}
});
}
In this case I got COMPLETE message, but I was expecting to get nothing because the second subject is not completed.
What I am doing wrong ?
Please help me to get desired behaviour.

Yes, it works as expected. It makes perfect sense to receive the onCompleted() here, because if one stream is done, as long as all the elements it emitted are "zipped", there's no way to "zip" anything more, so it's "completed". You can also play with the sequence here.

Related

How to convert this method AsyncTask to RxAndroid

I am new to rxAndroid. I use the following method with AsyncTask and I would like to convert it to RXjava.
mytask = new AsyncTask<long[], Void, Void>() {
Void doInBackground(long[] ... longs) {
for (long user : longs[0]) {
if (isCancelled())
return null;
try {
call(user);
} catch {
//
}
return null;
}
#Override
protected void onProgressUpdate(Void... voids) {
adapter.removeItem(0);
}
void onPreExecute() {
adapter.setBlocked(true);
}
void onPostExecute(Void aVoid) {
onStop();
}
void onCancelled() {
onStop();
}
void onStop() {
adapter.setBlocked(false);
}
}
.execute(adapter.getRemoveList());
}
I need help converting this AsyncTask to RxJava using the RxAndroid library.
Observable.just(input) // your input goes here
.map(input -> { //doInBackground with input goes here})
.subscribeOn(Schedulers.io())// take cares of doing task in background thread
.observeOn(AndroidSchedulers.mainThread())// make sures to deliver result on main thread
.doOnSubscribe(() -> {
// pre execute logic goes here
})
.subscribe(_ -> {
//post execute goes here
});

SwipeRefresh layout progress bar is not hiding even after completion

I am using MVVM pattern in which I am using SwipeRefresh layout to refresh recycler view in my layout.When I am pulling it then it continue to refresh even after method completed successfully.
Below is my code:
MainActivity.java
refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
userRepository.getUserList();
}
});
UserRepository.java
public void getUserList(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
Call<List<User>> userList = apiService.getUser();
userList.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, final Response<List<User>> response) {
Completable.fromAction(new Action() {
#Override
public void run() throws Exception {
if(response.body() != null) {
List<User> list = response.body();
for (int i = 0; i < list.size(); i++) {
String id = list.get(i).get_id();
String names = list.get(i).getName();
String age = list.get(i).getAge();
User user = new User(id,names,age);
userDb.userDao().Insert(user);
}
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onComplete() {
Toast.makeText(context,"Data inserted",Toast.LENGTH_SHORT).show();
}
#Override
public void onError(Throwable e) {
Toast.makeText(context,e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
Method fetching list is in another class UserRepository and I am calling method in another activity MainActivity.I am not getting any way how can I stop refreshing process.Someone please let me know a way to stop refreshing process.
Any help would be appreciated.
THANKS
To disable the progress dialog add this,
swipeLayout.setRefreshing(false);

RxJava2/RxAndroid Fetching information from website issue

I'm trying to explore Rxjava 2 and get some issue with fetching information from website. Actutally Observer give me an error.
My code:
observable.subscribe(getObserver());
Observable
Observable<String> observable = Observable.fromCallable(new Callable<String>() {
#Override
public String call() throws Exception {
String data = getHeadlines(SOURCE_WEB);
return data;
}
});
Observer
protected <String> Observer<String> getObserver() {
return new Observer<String>() {
#Override
public void onSubscribe(Disposable d) {
Log.i("Observer get: ", "mamy sub");
Toast.makeText(context, "Wait...", Toast.LENGTH_SHORT).show();
}
#Override
public void onNext(String value) {
Log.i("Observer get: ",value.toString());
}
#Override
public void onError(Throwable e) {
Log.i("Observer get ", "Error");
}
#Override
public void onComplete() {
Log.i("Observer ", "complete");
}
};
}
My fetching method
public String getHeadlines(String source) throws IOException {
Document doc = (Document) Jsoup.connect(source).get();
Elements newsHeadlines = doc.select("h2");
return newsHeadlines.toString();
}
When I'm using AsyncTask everything work fine...

how to make volley onResponse run in UnitTest thread

i'm making unit test for my application
my unit test class has this method
#Before
public void initialize() {
mContext = InstrumentationRegistry.getTargetContext();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(mContext).name("realmTest").inMemory().build();
Realm.setDefaultConfiguration(realmConfiguration);
mWorkoutsModel = new WorkoutsModel(mContext);
mRealm = Realm.getInstance(realmConfiguration);
mWorkoutsModel.registerListener(this);
}
#Test
public void getWorkouts() throws Exception {
mWorkoutsModel.onStart();
mLock.await();
mWorkoutsModel.onStop();
}
#After
public void deInitialize() {
mWorkoutsModel.unRegisterListener();
mRealm.close();
}
and my model
#Override
public void onStart() {
mRealm = Realm.getDefaultInstance();
getDataFromApi();
}
private boolean getDataFromApi() {
Constants.AllAPIs.ALLWorkouts allWorkouts = new Constants.AllAPIs.ALLWorkouts();
if (Permissions.isInternetConnectionExist(mContext)) {
mApiHandler.downLoadDataFromApi(AllWorkouts.class, allWorkouts.getBaseUrl(),
new APIHandler.StringResponseHandler<AllWorkouts>() {
#Override
public void onResponse(AllWorkouts response) {
insertWorkouts(response.getWorkouts());
},
new APIHandler.ErrorResponseHandler() {
#Override
public void onErrorResponse(VolleyError error) {
}
}, TAG);
return true;
} else {
return false;
}
}
private void insertWorkouts(final List<Workout> workouts) {
mCurrentInsertTransaction = mRealm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm bgRealm) {
bgRealm.copyToRealmOrUpdate(workouts);
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
}
});
}
my problem that the unittest calls onStart which create realm object in the model in test thread but volley force onResponse to run on UIThread which makes realm throw exception Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
the code runs perfect in normal, but in test it fails
does anyone faced same problem or can solve it ?
i solved my problem by run the test in handler
new Handler(mContext.getMainLooper()).post(new Runnable() {
#Override
public void run() {
try {
mWorkoutsModel.onStart();
mLock.await();
mWorkoutsModel.onStop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
hope that help somebody

Android AsyncTask runs multiple times

In my app ,there is an one button which get input from database.When I press it more than one in a short time it crashes.
How can i avoid this error with using asynctask?
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showinf();
}
});
}
private String[] columns={"name","surname"};
private void showinf(){
SQLiteDatabase db=v1.getReadableDatabase();
Cursor c=db.query("infos",columns,null,null, null,null,null);
Random mn2=new Random();
int count=c.getCount();
String mn=String.valueOf(count);
int i1=mn2.nextInt(count+1);
c.move(i1);
t1.setText(c.getString(c.getColumnIndex("name")));
t2.setText(c.getString(c.getColumnIndex("surname")));
}
thanks...
You can create a boolean flag (let's say bDiscardButtonAction), and set it to true in onPreExecute() and set it to false in onPostExecute(), something like:
public class FooTask extends AsyncTask<Foo, Foo, Foo>
{
private static boolean bDiscardButtonAction = false;
private boolean isDiscareded = false;
#Override
public void onPreExecute()
{
if(bDiscardButtonAction)
{
isDiscareded = true;
return;
}
bDiscardButtonAction = true;
}
#Override
public Foo doInBackground(Foo... params)
{
if(isDiscareded) return;
// ...
}
#Override
public void onPostExecute(Void result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
#Override
public void onCancelled(Foo result)
{
if(!isDiscareded) bDiscardButtonAction = false;
}
}
disable the show button in onPreExecute() and enable it back onPostExecute().
public class getAsyncDataTask extends AsyncTask<Void, Void, Void>
{
#Override
public void onPreExecute()
{
show.setAlpha(0.5);
show.setEnable(false);
}
#Override
public void doInBackground(Void... params)
{
//retrieve the data from db;
}
#Override
public void onPostExecute()
{
show.setAlpha(1.0);
show.setEnable(true);
}
}
I hope this code will help u out.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
new AsynchTaskManualLocation().execute();
});
public class AsynchTaskGetData extends AsyncTask<String,Void,String>
{
#Override
protected String doInBackground(String... url) {
//showinf(); this method contains operation of getting data from //database OR ur logic to getdata
return showinf();
}
#Override
protected void onPostExecute(String result) {
//here u get result in resul var and process the result here
}
}
}

Categories