DevKRos
DevKRos

Reputation: 432

What is best approach to manage concurrent network calls (http client) to load multiple fragments(loosely coupled) data inside single activity?

I am working on session based App using kaltura resftful APis.

The design is such i have a view pager+tablayout in each page i have a fragment and all fragment is bound to single activity.

In splash i connect to Api and create a sesssion now i have multiple network calls (and all must be done in async task) .

Problem statement:

Let's say i have C1,C2,C3,C4,C5,C6 fragments and

every C has it's own set of data(Via n/w call,asynctask ) loading data blocks main thread and if user swipes to another page a new asynctask is executed while previous background thread is still loading which causes lags and ANR.

Suggestion:

What is best approach to manage each fragment's network call : should i create thread pooler for these async tasks around 8-10 and start thread pooler in splash.

I am very keen on activity's memory leak and want to achieve fast loading of viewpager(fragment's views) i read many SO questions and @commonsWare Blog and also

this discussion on thread pooler but want to know better approach/architecture of doing above stated question.

Upvotes: 0

Views: 1114

Answers (1)

Janusz Hain
Janusz Hain

Reputation: 617

Try RxJava. It is better approach than Async task as Async task isn't making several network calls at once. RxJava is good (but hard to learn) library which can be used for easier multithreading.

You can use Retrofit with it, so it is not a problem.

With kaltura API you will have to play around.

For making multiple calls at once you can use merge function with several calls, so all calls will be called at once.

You can also call each Observable when creating new fragment, if done right RxJava will take care of threading, so "old" Observable will not stop and new one will be still loading new data.

Also zip function can be handy, as it can load few sources into "one" object.

Also there is link to my github project (sorry about no readability of it):

https://github.com/JanuszHain/SocialMediaWatcher

Files that might interest you (data loading using RxJava with API that doesn't support RxJava in a straight way):

Using API library (Twitter) to get data with callback and then creating Observable out of it (for example function private Observable<ArrayList<Tweet>> createObservableReadTweets(final String screen_name, final int count)): https://github.com/JanuszHain/SocialMediaWatcher/blob/master/app/src/main/java/pl/janusz/hain/socialmediawatcher/TwitterTimelineGetter.java

Subscribing to created Observable (function observableGetNewTweets()). Note that I created new Observable out of Observable in this function, so it may be a little complicated: https://github.com/JanuszHain/SocialMediaWatcher/blob/master/app/src/main/java/pl/janusz/hain/socialmediawatcher/TwitterTimeline.java

Creating ArrayList of Observables and then merging them (creating multiple calls at once): https://github.com/JanuszHain/SocialMediaWatcher/blob/master/app/src/main/java/pl/janusz/hain/socialmediawatcher/TwitterWall.java

And here is my Scheduler. It is for Observable config, limiting amount of threads for optimizing large amounts of new threads requests: https://github.com/JanuszHain/SocialMediaWatcher/blob/master/app/src/main/java/pl/janusz/hain/socialmediawatcher/util/MyScheduler.java

Upvotes: 0

Related Questions