1

My android app supports multiple user logins. I user Dagger2 to maintain the UserScope dependencies which get created and destroyed as user switch happens. One of these dependencies is an OkHttpClient for every user, with its own authentication/cookies.

I am looking to use Fresco for my app, but I am not able to find a way where I can switch the OKHttpClient at runtime in Fresco, since the initialization only happens once for it. From the documentation :

OkHttpClient okHttpClient; //my per user session client ImagePipelineConfig config = OkHttpImagePipelineConfigFactory .newBuilder(context, okHttpClient) .build(); Fresco.initialize(context, config);

Any ideas to achieve this multi user scenario with Fresco ?

duskandawn
  • 646
  • 1
  • 10
  • 19
  • 1
    "its own authentication/cookies" Can you pop/modify these when you need to? Alternatively, can you swap Fresco clients when you need to (a new Fresco client with a new OkHttpClient)? You could probably prefill the new Fresco client with the old one's memory cache and such, too. – Eric Cochran Mar 30 '17 at 22:42
  • "Alternatively, can you swap Fresco clients when you need to (a new Fresco client with a new OkHttpClient)" This is what I was looking for, how can do this with Fresco. I thought he only initialization in the app is this call "Fresco.initialize(context, config)" how can I override it without killing the app ? – duskandawn Mar 30 '17 at 22:56
  • I'm not a Fresco user, so I'm not sure. I had a look at the API, and it looks like it doesn't give you control of the client. amazing. Probably file a feature request. Sorry. – Eric Cochran Mar 30 '17 at 23:53
  • You might be able to make an OkHttp interceptor that calls into another OkHttpClient instance altogether. It's weird but should just work. – Jesse Wilson Mar 31 '17 at 04:35
  • Whoa, that's super cool. You could have one OkHttpClient that "delegates" to a swappable OkHttpClient in an Interceptor. When you want to swap users, swap out the OkHttpClient in the Interceptor. nice! – Eric Cochran Mar 31 '17 at 23:46
  • 2
    ... and @JesseWilson has a post on it now. haha! https://publicobject.com/2017/04/02/a-clever-flawed-okhttp-interceptor-hack/ – Eric Cochran Apr 02 '17 at 17:40
  • 1
    Thanks guys, i just moved to using `Picasso` for my requirements, as it had a much cleaner interface with OKHttp to achieve this. – duskandawn Apr 02 '17 at 19:35

1 Answers1

0

You can simply create a custom network fetcher that holds multiple OkHttpClients and change them when needed or create a new fetcher that holds multiple OPkHttpNetworkFetchers.

Just use the original OkHttpNetworkFetcher code as a reference: https://github.com/facebook/fresco/blob/master/imagepipeline-backends/imagepipeline-okhttp3/src/main/java/com/facebook/imagepipeline/backends/okhttp3/OkHttpNetworkFetcher.java

Alexander Oprisnik
  • 1,212
  • 9
  • 9