【发布时间】:2017-12-28 22:48:43
【问题描述】:
在这些对其他 stackoverflow 问题的回答中:https://stackoverflow.com/a/41360829/1932522 和 https://stackoverflow.com/a/40557237/1932522 提供了一些示例,展示了如何利用 TaskCompletionSource 在任务中运行延迟调用。
当在一个简单的例子中实现它时(下面提供),我遇到了.continueWith( new TaskerB() )); 行将无法编译的问题,因为编译器需要一个任务作为第一个参数:Incompatible equality constraint: Task<String> and String,我希望这个参数应该只有字符串类型?谁能帮助让这段代码工作并解释我如何成功使用TaskCompletionSource。
注意:这个例子很简单,在实际使用中我会运行一个 Firestore 动作,设置一个监听器并从监听器内部调用tcs.setResult(..)。
public class StartTask implements Callable<Integer>{
@Override
public Integer call() throws Exception {
return 1;
}
}
public class TaskerA implements Continuation< Integer, Task<String>>{
@Override
public Task<String> then(@NonNull Task<Integer> task) throws Exception {
final TaskCompletionSource<String> tcs = new TaskCompletionSource<>();
tcs.setResult( "value: " + task.getResult() );
return tcs.getTask();
}
}
public class TaskerB implements Continuation< String, Void>{
@Override
public Void then(@NonNull Task<String> task) throws Exception {
Log.d(TAG, "Output is: " + task.getResult());
return null;
};
}
public void runExample(){
Tasks.call( new StartTask() )
.continueWith( new TaskerA() )
.continueWith( new TaskerB() ));
}
【问题讨论】:
标签: java android firebase google-play-services