改造同步请求和异步请求有什么区别?哪个更好,为什么? [英] what is the difference between retrofit synchronous and asynchronous request? which one is better and why?

查看:77
本文介绍了改造同步请求和异步请求有什么区别?哪个更好,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的在每个地方都进行了搜索,可以发出同步和异步数据请求,但是我实际上无法理解哪个异步对象与什么异步对象?以及什么与什么同步?

I have really searched for this every where, I can make both synchronous and asynchronous data requests, but I can't actually understand which is asynchronous with what? and what is sync with what?

推荐答案

当您异步时,它意味着不在前台(它在完成给定任务时不会阻塞用户界面),另一方面,在同步时,它意味着前台,而您的应用程序在UI消耗的同一线程中执行操作.

when you asynchronous, it means not in the foreground(it does not block the users interface while it accomplishes the given task), on other hand synchronous means in the foreground while your application execute things in the same thread the UI consuming.

在您的情况下(通过改型或任何其他REST API发出REST请求),您不得在该前台中进行该操作,而必须在后台线程中进行该操作.

In your case(making REST requests via retrofit or any other REST api) you must not make that in that foreground and you have to make in a background thread.

在进行改装的情况下,您可以通过以下方法提出请求:

In the case of retrofit you have the following methods to make the request:

call.execute() // works in the foreground.
call.enqueue() // works in the background.

因此,您有两种选择:直接拨打电话或入队,也可以使用call.execute但用服务包装(我要亲自处理后台工作).

So you have a choice of two: either you make the call.enqueue directly or you can user call.execute but wrapped with a service(I mean you have to handle the background work your self).

这篇关于改造同步请求和异步请求有什么区别?哪个更好,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆