RxAndroid Retrofit how to fetch 2nd list only when 1st list emits are completed?












0















Take a simple example given 2 lists Categories and Products



I need to sync on remote server list of local categories and store the returned remote_id on category item,
after that start sync on remote server list of products and store foreach remote_id on product item.




All categories need to sync all before start sync products, because
when sync products i need category id given from server




Main goal is




  1. loop for each item on "categories" list

  2. make API call for each item

  3. ONLY when ALL api call are completed (because i need to get all rempote_id before start to sync products), start same loop on "products" list


In my example use the onComplete are wrong as it get fired when all list items are fetched and NOT when all api calls are completed.



How to solve? What operator should use?



... from ApiService.java
// Create category
@FormUrlEncoded
@POST("Category/")
Single<ApiResponse> createCategory(@Field("Description") String description, @Field("ShortDescription") String shortDescription);

@FormUrlEncoded
@POST("Product/")
Single<ApiResponse> createProduct(@Field("ProductCode") String ProductCode,
@Field("Description") String Description,
@Field("ShortDescription") String ShortDescription,
@Field("Quantity") int Quantity,
@Field("MeasurementUnit_ID") int MeasurementUnit_ID,
@Field("SellPrice") int SellPrice,
@Field("Category_ID") int Category_ID,
@Field("Vat_ID") int Vat_ID
);


....
// MainActivity
List<Category> cats = new CategoryOperations(this).getUnsynced();
List<Product> prods = new ProductOperations(this).getUnsynced();

Observable.fromIterable(cats)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Category>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Category c) {
apiService.createCategory(c.getDescription(), c.getDescription())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<ApiResponse>() {
@Override
public void onSuccess(ApiResponse resp) {
if (resp.id > 0 && resp.success) {
//Toast.makeText(getApplicationContext(), "Created category id: " + resp.id, Toast.LENGTH_LONG).show();
Log.d(TAG, "Created category id: " + resp.id );
c.setCloudId(resp.id);
new CategoryOperations(ctx).update(c);
return;
}else{
Log.d(TAG, "Category not created " + resp.message);
}
}

@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
}
);
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {
Log.d(TAG, "All categories synced");
Log.d(TAG, "Now can start to sync products");
Log.d(TAG, "Obvioussly error here as this complete only fired when all items are fetched and NOT");
Log.d(TAG, "When all api calls are completed");
}
});









share|improve this question























  • Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

    – akarnokd
    Nov 13 '18 at 13:13











  • Do you solve your problem?

    – ConstOrVar
    Nov 13 '18 at 16:48
















0















Take a simple example given 2 lists Categories and Products



I need to sync on remote server list of local categories and store the returned remote_id on category item,
after that start sync on remote server list of products and store foreach remote_id on product item.




All categories need to sync all before start sync products, because
when sync products i need category id given from server




Main goal is




  1. loop for each item on "categories" list

  2. make API call for each item

  3. ONLY when ALL api call are completed (because i need to get all rempote_id before start to sync products), start same loop on "products" list


In my example use the onComplete are wrong as it get fired when all list items are fetched and NOT when all api calls are completed.



How to solve? What operator should use?



... from ApiService.java
// Create category
@FormUrlEncoded
@POST("Category/")
Single<ApiResponse> createCategory(@Field("Description") String description, @Field("ShortDescription") String shortDescription);

@FormUrlEncoded
@POST("Product/")
Single<ApiResponse> createProduct(@Field("ProductCode") String ProductCode,
@Field("Description") String Description,
@Field("ShortDescription") String ShortDescription,
@Field("Quantity") int Quantity,
@Field("MeasurementUnit_ID") int MeasurementUnit_ID,
@Field("SellPrice") int SellPrice,
@Field("Category_ID") int Category_ID,
@Field("Vat_ID") int Vat_ID
);


....
// MainActivity
List<Category> cats = new CategoryOperations(this).getUnsynced();
List<Product> prods = new ProductOperations(this).getUnsynced();

Observable.fromIterable(cats)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Category>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Category c) {
apiService.createCategory(c.getDescription(), c.getDescription())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<ApiResponse>() {
@Override
public void onSuccess(ApiResponse resp) {
if (resp.id > 0 && resp.success) {
//Toast.makeText(getApplicationContext(), "Created category id: " + resp.id, Toast.LENGTH_LONG).show();
Log.d(TAG, "Created category id: " + resp.id );
c.setCloudId(resp.id);
new CategoryOperations(ctx).update(c);
return;
}else{
Log.d(TAG, "Category not created " + resp.message);
}
}

@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
}
);
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {
Log.d(TAG, "All categories synced");
Log.d(TAG, "Now can start to sync products");
Log.d(TAG, "Obvioussly error here as this complete only fired when all items are fetched and NOT");
Log.d(TAG, "When all api calls are completed");
}
});









share|improve this question























  • Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

    – akarnokd
    Nov 13 '18 at 13:13











  • Do you solve your problem?

    – ConstOrVar
    Nov 13 '18 at 16:48














0












0








0








Take a simple example given 2 lists Categories and Products



I need to sync on remote server list of local categories and store the returned remote_id on category item,
after that start sync on remote server list of products and store foreach remote_id on product item.




All categories need to sync all before start sync products, because
when sync products i need category id given from server




Main goal is




  1. loop for each item on "categories" list

  2. make API call for each item

  3. ONLY when ALL api call are completed (because i need to get all rempote_id before start to sync products), start same loop on "products" list


In my example use the onComplete are wrong as it get fired when all list items are fetched and NOT when all api calls are completed.



How to solve? What operator should use?



... from ApiService.java
// Create category
@FormUrlEncoded
@POST("Category/")
Single<ApiResponse> createCategory(@Field("Description") String description, @Field("ShortDescription") String shortDescription);

@FormUrlEncoded
@POST("Product/")
Single<ApiResponse> createProduct(@Field("ProductCode") String ProductCode,
@Field("Description") String Description,
@Field("ShortDescription") String ShortDescription,
@Field("Quantity") int Quantity,
@Field("MeasurementUnit_ID") int MeasurementUnit_ID,
@Field("SellPrice") int SellPrice,
@Field("Category_ID") int Category_ID,
@Field("Vat_ID") int Vat_ID
);


....
// MainActivity
List<Category> cats = new CategoryOperations(this).getUnsynced();
List<Product> prods = new ProductOperations(this).getUnsynced();

Observable.fromIterable(cats)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Category>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Category c) {
apiService.createCategory(c.getDescription(), c.getDescription())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<ApiResponse>() {
@Override
public void onSuccess(ApiResponse resp) {
if (resp.id > 0 && resp.success) {
//Toast.makeText(getApplicationContext(), "Created category id: " + resp.id, Toast.LENGTH_LONG).show();
Log.d(TAG, "Created category id: " + resp.id );
c.setCloudId(resp.id);
new CategoryOperations(ctx).update(c);
return;
}else{
Log.d(TAG, "Category not created " + resp.message);
}
}

@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
}
);
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {
Log.d(TAG, "All categories synced");
Log.d(TAG, "Now can start to sync products");
Log.d(TAG, "Obvioussly error here as this complete only fired when all items are fetched and NOT");
Log.d(TAG, "When all api calls are completed");
}
});









share|improve this question














Take a simple example given 2 lists Categories and Products



I need to sync on remote server list of local categories and store the returned remote_id on category item,
after that start sync on remote server list of products and store foreach remote_id on product item.




All categories need to sync all before start sync products, because
when sync products i need category id given from server




Main goal is




  1. loop for each item on "categories" list

  2. make API call for each item

  3. ONLY when ALL api call are completed (because i need to get all rempote_id before start to sync products), start same loop on "products" list


In my example use the onComplete are wrong as it get fired when all list items are fetched and NOT when all api calls are completed.



How to solve? What operator should use?



... from ApiService.java
// Create category
@FormUrlEncoded
@POST("Category/")
Single<ApiResponse> createCategory(@Field("Description") String description, @Field("ShortDescription") String shortDescription);

@FormUrlEncoded
@POST("Product/")
Single<ApiResponse> createProduct(@Field("ProductCode") String ProductCode,
@Field("Description") String Description,
@Field("ShortDescription") String ShortDescription,
@Field("Quantity") int Quantity,
@Field("MeasurementUnit_ID") int MeasurementUnit_ID,
@Field("SellPrice") int SellPrice,
@Field("Category_ID") int Category_ID,
@Field("Vat_ID") int Vat_ID
);


....
// MainActivity
List<Category> cats = new CategoryOperations(this).getUnsynced();
List<Product> prods = new ProductOperations(this).getUnsynced();

Observable.fromIterable(cats)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Category>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(Category c) {
apiService.createCategory(c.getDescription(), c.getDescription())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<ApiResponse>() {
@Override
public void onSuccess(ApiResponse resp) {
if (resp.id > 0 && resp.success) {
//Toast.makeText(getApplicationContext(), "Created category id: " + resp.id, Toast.LENGTH_LONG).show();
Log.d(TAG, "Created category id: " + resp.id );
c.setCloudId(resp.id);
new CategoryOperations(ctx).update(c);
return;
}else{
Log.d(TAG, "Category not created " + resp.message);
}
}

@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
}
);
}

@Override
public void onError(Throwable e) {

}

@Override
public void onComplete() {
Log.d(TAG, "All categories synced");
Log.d(TAG, "Now can start to sync products");
Log.d(TAG, "Obvioussly error here as this complete only fired when all items are fetched and NOT");
Log.d(TAG, "When all api calls are completed");
}
});






retrofit2 rx-java2 rx-android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 12:10









Marco LuongoMarco Luongo

13719




13719













  • Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

    – akarnokd
    Nov 13 '18 at 13:13











  • Do you solve your problem?

    – ConstOrVar
    Nov 13 '18 at 16:48



















  • Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

    – akarnokd
    Nov 13 '18 at 13:13











  • Do you solve your problem?

    – ConstOrVar
    Nov 13 '18 at 16:48

















Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

– akarnokd
Nov 13 '18 at 13:13





Suggested reading: github.com/ReactiveX/RxJava#dependent-sub-flows

– akarnokd
Nov 13 '18 at 13:13













Do you solve your problem?

– ConstOrVar
Nov 13 '18 at 16:48





Do you solve your problem?

– ConstOrVar
Nov 13 '18 at 16:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280753%2frxandroid-retrofit-how-to-fetch-2nd-list-only-when-1st-list-emits-are-completed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280753%2frxandroid-retrofit-how-to-fetch-2nd-list-only-when-1st-list-emits-are-completed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python