异步获取接口方法的返回值(无参数)。可以实现吗?

问题描述 投票:0回答:1

我已经获得了这样的界面:

MyInterface {
    List<FirebaseVisionFace> getFaceList();
}

我必须在类中实现它(我们称其为MyFirebaseFaceClass),这样我们就可以做到

List<FirebaseVisionFace> faceList = myFirebaseFaceClass.getFaceList()

问题是要获得此面孔列表,需要以下内容:

Task<List<FirebaseVisionFace>> result =
    detector.detectInImage(image)
            .addOnSuccessListener(
                    new OnSuccessListener<List<FirebaseVisionFace>>() {
                        @Override
                        public void onSuccess(List<FirebaseVisionFace> faces) {
                            // Task completed successfully
                            // ...
                        }
                    })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            // Task failed with an exception
                            // ...
                        }
                    });

是否可以在不将回调作为参数传递而使用回调获取列表的情况下实现此getFaceList()方法?

java asynchronous interface callback
1个回答
0
投票
Task<List<FirebaseVisionFace>> result = detector.detectInImage(image) .addOnSuccessListener((List<FirebaseVisionFace> faces) -> { // Task completed successfully }) .addOnFailureListener((@NotNull Exception e) -> { // Task failed with an exception })
© www.soinside.com 2019 - 2024. All rights reserved.