1

This is one of the APIs that working with Retrofit library.

 @GET("users/self/media/recent/")
        Call<List<Media>> getRecentPostList(@Query("access_token") String access_token);

I'm using GsonConverterFactory to output the result, and this will return a List<> object, but can I synchronize the List?

What I'm trying is to use Collections.synchronizedList(new ArrayList<>()), and pass the result list of Gson as a parameter to the constructor of the ArrayList.

Is there any better way?

Community
  • 1
  • 1
OOD Waterball
  • 707
  • 1
  • 11
  • 31
  • Just curious, if I'm getting you right: you're going to return a synchronized list as a result of deserialization -- why do you need this? (Synchronized data structures have extra cost, and I'd think that you'd need to make it synchronized in place). – Lyubomyr Shaydariv Jun 07 '17 at 16:14
  • yes, because then I am going to start lots of threads for doing lots of asynchronous network tasks with changing the list, if the list is not synchronized the data will go inconsistent. – OOD Waterball Jun 07 '17 at 16:17
  • Yep, it may be inconsistent. I was just curious wouldn't it be better to make lists synchronized right before use, and not as a deserialization result? – Lyubomyr Shaydariv Jun 07 '17 at 16:48
  • Umm..What did you mean? Now I'm using Collections.synchronizedList() method with the deserialized List from Retrofit, isn't it synchronized before use? – OOD Waterball Jun 07 '17 at 19:12
  • 1
    Yeah, it is. I just think that it is fine as you have it now. – Lyubomyr Shaydariv Jun 07 '17 at 20:26

0 Answers0