Does Retrofit make network calls on main thread?

前端 未结 3 771
轮回少年
轮回少年 2020-11-30 01:03

I am trying to explore Retrofit+OkHttp on Android. Here\'s some code I found online :

RestAdapter restAdapter = new RestAdapter.Builder().setExecutors(execu         


        
相关标签:
3条回答
  • 2020-11-30 01:46

    Kotlin Coroutines

    When using a suspend function to make a network request, Retrofit uses a Coroutine CallAdapter to dispatch on a worker thread.

    Therefore, a coroutine does not need to be explicitly launched on the Dispatcher.IO thread.

    See Android documentation: Page from network and database > Implement a RemoteMediator

    Sample

    Injection.kt

    object Injection {
        val feedService = Retrofit.Builder()
            .baseUrl(TWITTER_API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(FeedService::class.java)
    }
    

    FeedService.kt

    interface FeedService {
        // Retrofit makes the request on a background thread.
        @GET("lists/{listType}")
        suspend fun getTweets(
            @Path(LIST_TYPE_PATH) listType: String,
            @Query(LIST_ID_QUERY) listId: String,
            @Query(LIST_COUNT_QUERY) count: String,
            @Query(LIST_PAGE_NUM_QUERY) page: String
        ): List<Tweet>
    }
    

    FeedPagingSource

    class FeedPagingSource : PagingSource<Int, Tweet>() {
        override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Tweet> {
            try {
                // The results are returned on the main thread.
                val tweets: List<Tweet> = Injection.feedService.getTweets(...)
                return LoadResult.Page(...)
            } catch (error: Exception) { ... }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 01:49

    The method that return a value does it Synchronously.

    @GET("/user/{id}/asset")
    Asset getUserAsset(@Path("id") int id);
    

    To do it Asynchronous all you need is to add a Callback.

    @GET("/user/{id}/asset")
    void getUserAsset(@Path("id") int id, Callback<Asset> cb);
    

    Hope this Helps.

    Regards!

    0 讨论(0)
  • 2020-11-30 02:01

    Retrofit methods can be declared for either synchronous or asynchronous execution.

    A method with a return type will be executed synchronously.

    @GET("/user/{id}/photo")
    Photo getUserPhoto(@Path("id") int id);
    

    Asynchronous execution requires the last parameter of the method be a Callback.

    @GET("/user/{id}/photo")
    void getUserPhoto(@Path("id") int id, Callback<Photo> cb);
    

    On Android, callbacks will be executed on the main thread. For desktop applications callbacks will happen on the same thread that executed the HTTP request.

    Retrofit also integrates RxJava to support methods with a return type of rx.Observable

    @GET("/user/{id}/photo")
    Observable<Photo> getUserPhoto(@Path("id") int id);
    

    Observable requests are subscribed asynchronously and observed on the same thread that executed the HTTP request. To observe on a different thread (e.g. Android's main thread) call observeOn(Scheduler) on the returned Observable.

    Note: The RxJava integration is experimental.

    0 讨论(0)
提交回复
热议问题