Skip to content

Use Call.Factory instead of OkHttpClient directly#293

Merged
wzieba merged 2 commits intotrunkfrom
mehow/lazy-http
Jan 22, 2026
Merged

Use Call.Factory instead of OkHttpClient directly#293
wzieba merged 2 commits intotrunkfrom
mehow/lazy-http

Conversation

@MiSikora
Copy link
Contributor

Using Call.Factory instead of OkHttpClient allows library consumers to instantiate OkHttpClient lazily if desired. This is important because OkHttpClient initialization can take upwards of 100 ms on some Android devices, potentially causing ANRs or slowing down app startup.

This change is binary-compatible, as Call.Factory is a superinterface of OkHttpClient. However, it may be source-incompatible for Kotlin consumers that rely on named parameters, since the constructor parameter was renamed from okhttpClient to callFactory.

If this is considered a deal-breaker, I can revert the parameter name change. However, I do not think it is a significant issue, as this dependency is typically instantiated only once in most applications, making the migration effort minimal. We can also explicitly call this out in the changelog.

@MiSikora MiSikora requested a review from wzieba January 22, 2026 11:02
Copy link
Member

@wzieba wzieba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM

If this is considered a deal-breaker, I can revert the parameter name change. However, I do not think it is a significant issue, as this dependency is typically instantiated only once in most applications, making the migration effort minimal. We can also explicitly call this out in the changelog.

I think it's totally fine - IMO it's not a deal breaker, especially for internal-only library.


Just for a reference, would you mind linking on this PR, a PR from Pocket Casts that will introduce lazy initialization of OkHttpClient ? I think it can be a valuable resource for any consumer of the lib.

@MiSikora
Copy link
Contributor Author

MiSikora commented Jan 22, 2026

Just for a reference, would you mind linking on this PR, a PR from Pocket Casts that will introduce lazy initialization of OkHttpClient ? I think it can be a valuable resource for any consumer of the lib.

Do you mean for me to make a PR with changes in Pocket Casts first against the build from the 7985925 commit and then link it here before merging this PR? If yes, then no problem.

@MiSikora MiSikora changed the title Use call factory instead of OkHttp client directly Use Call.Factory instead of OkHttpClient directly Jan 22, 2026
@wzieba
Copy link
Member

wzieba commented Jan 22, 2026

I think that we can do this after integrating the new, stable release to Pocket Casts - not necceseriely against the build from 7985925 commit 👍

Let me release new version of Tracks now

@wzieba wzieba merged commit 18dcfc6 into trunk Jan 22, 2026
11 checks passed
@wzieba wzieba deleted the mehow/lazy-http branch January 22, 2026 12:48
@MiSikora
Copy link
Contributor Author

MiSikora commented Jan 26, 2026

So there are two related changes in Pocket Casts:

  • Async Retrofit dispatcher. This is needed to ensure that whenever a call is enqueued via a Retrofit instance, the call factory is invoked on a background thread. Otherwise, it is called on the caller thread, which very often is the main thread. However, this only works with functions that are suspending or return Call<Foo>. If a Retrofit interface returns something like Observable, CompletableFuture, etc., you need to make sure that the call adapter you are using dispatches the call on a background thread, or write an adapter for that type.

  • Lazy OkHttp. This change ensures that all calls that require creating OkHttp or Cache are performed on a background thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants