java - 成功和失败侦听器方法是否在后台线程上完成?
问题描述
使用成功和失败侦听器的方法是在主 UI 线程还是后台线程上完成的?
例如,我使用的是Google Places SDK。获取地点:
// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";
// Specify the fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Construct a request object, passing the place ID and fields array.
final FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
Place place = response.getPlace();
Log.i(TAG, "Place found: " + place.getName());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + exception.getMessage());
final int statusCode = apiException.getStatusCode();
// TODO: Handle error with given status code.
}
});
fetchPlace()
在后台线程中完成?
解决方案
啊,我做了一些挖掘,这就是我想出的。
placesClient.fetchPlace(...)
返回一个任务:
这对我们来说意味着你可以将它连接到不同的监听器,并在该状态被击中时得到一个 ping。在您选择的特定addOnSuccessListener(...)
方法的情况下,这是文档告诉我们的内容:
所以基本上,在你的代码片段中,获取将在主线程之外完成,然后结果将被传递回主线程。
推荐阅读
- linear-programming - 如何使用二进制变量在 LPsolve 中添加依赖约束
- reactjs - 为什么在渲染组件之前触发 useEffect() ?
- hyperlink - target ="_blank" 自动添加到电子邮件中
- html - HTML -> 固定顶部,具有最小高度和动态平方内容的灵活底部
- javascript - 调用 componentDidUpdate() 时页面会短暂冻结
- python - 我正在对黄金配置与应该与黄金配置相同的其他文件目录进行差异+比较
- pandas - value_counts 前 20 条记录
- php - Woocommerce rest api 类别和图片上传/添加问题
- mongodb - 时间:MongoDB 中的日期和时间
- qml - 创建新的自定义 QtQuick 视图以与模型一起使用