如何在FirebaseDatabase上运行Multiples Task

时间:2017-04-20 08:29:08

标签: android firebase firebase-realtime-database

我创建了两个在firebase数据库上写入数据的任务

class EventDataAddingTask implements Continuation<User, Task<Boolean>> {
        @Override
        public Task<Boolean> then(Task<User> task) {
            final TaskCompletionSource<Boolean> tcs = new TaskCompletionSource();
            mUserEventsRefrence.child(user.getUid()).setValue(addEditEventData)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                tcs.setResult(true);
                            }
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception error) {
                            tcs.setResult(false);
                            tcs.setException(error);
                        }
                    });
            return tcs.getTask();
        }
    }



class UserPaymentAddingTask implements Continuation<User, Task<Boolean>> {
        @Override
        public Task<Boolean> then(Task<User> task) {
            final TaskCompletionSource<Boolean> tcs = new TaskCompletionSource();
            mUserPaymentDetailsRefrence.child(user.getUid()).setValue(addEditUserPaymentData)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                tcs.setResult(true);
                            }
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception error) {
                    tcs.setResult(false);
                    tcs.setException(error);
                }
            });
            return tcs.getTask();
        }
    }

我不知道如何运行这两项任务,以便在一项任务完成后自动运行并在两项任务成功完成时向用户显示消息

1 个答案:

答案 0 :(得分:1)

Play Services Task API使用chaincontinueWithTask()任务提供了一种方法。