Java의 비동기 호출에 시간 초과 추가
-
21-12-2019 - |
문제
단지 Firebase 질문이 아니지만 Firebase를 사용하여 Android에서 백엔드에 게시를하고 10 번, 매 초마다 10 번 실행하고 있습니다.
Firebase ref = new Firebase(URL);
ref.child("Time").setValue(Currentime());
.
그러나 이것은 비동기식 호출이며 잠시 루프를 넣을 때 :
while (time_now < time_start + 10 seconds) {
Firebase ref = new Firebase(URL);
ref.child("Time").setValue(Currentime());
}
.
WHILE 루프를 처음 실행 한 다음 끝에 ~ 10 개의 Firebase 호출을 실행하는 것 같습니다.다음 비동기 (Firebase)가 다음 비동기 호출을 호출하기 전에 잠시 동안 실행되도록 호출 할 수 있도록 시간 초과를 추가하는 방법이 있습니까?
해결책
Firebase 웹 사이트 의 Java 예제를 보면doTransactions
메소드와 onComplete
메소드가있는 것을 알 수 있습니다.
Firebase countRef = new Firebase(URL);
Transaction.Handler handler = new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData currentData) {
// set the new time
...
}
@Override
public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {
if (error != null) {
...
} else {
if (!committed) {
...
} else {
// transaction committed, do next iteration
...
}
...
}
}
});
countRef.runTransaction(handler);
.
doTransaction
메소드에서 새 시간을 설정합니다.
// set the new time
currentData.setValue(time_now);
return Transaction.success(currentData);
.
을 누른 다음 onComplete
메소드에서 다음 반복을 시작합니다.
// transaction committed, do next iteration
if (time_now < time_start + 10 seconds) {
countRef.runTransaction(handler);
}
. 제휴하지 않습니다 StackOverflow