0

I have an activity which contains 3 fragments which make a network call to fetch some data,for example user details.The calls made by the 3 fragments are independent of each other but are still pulling the same data. Suppose all 3 fragments request for the data at the same time but fragment 1 makes the call first,is there a way fragment 2 and fragment 3 can check that the call was made for the same data and use that data when the response comes in rather than making their own call for the same data.

For example fragment 1 and 2 pull a list of data and for each data item there is an associated person who's details need to be fetched .I have written code to cache the persons data locally but on the first run there is no cache in which case both fragments make the call to fetch the persons data,there is no relation between the 2 fragment except for the person details.

I am using retrofit and rxjava in my project.

Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • 1
    "The calls made by the 3 fragments are independent of each other but are still pulling the same data" -- that's a bug, not a feature. IMHO, none of your fragments should be making the Retrofit call. Instead, isolate that logic into something else (e.g., a view model), owned by the activity and accessed by the fragments. All three fragments can subscribe to the data from the view model; the view model can make one Retrofit request. – CommonsWare Jul 08 '17 at 12:00
  • @CommonsWare fragment 1 and 2 pull a list of data and for each data item there is an associated person who's details need to be fetched .I have written code to cache the persons data locally but on the first run there is no cache in which case both fragments make the call to fetch the persons data,there is no relation between the 2 fragment except for the person details.I am chaining rxjava operators in both places to make these requests and as such doesn't seem possible to unify the request into a single view model – Jude Fernandes Jul 08 '17 at 12:09

0 Answers0