Javaの非同期呼び出しへのタイムアウトを追加する
-
21-12-2019 - |
質問
Firebaseの質問だけではなく、私はAndroidからのバックエンドに投稿するためにFirebaseを使用しています。毎秒10回実行します。
Firebase ref = new Firebase(URL);
ref.child("Time").setValue(Currentime());
.
しかしこれは非同期呼び出しであり、whileループを置くと:
while (time_now < time_start + 10 seconds) {
Firebase ref = new Firebase(URL);
ref.child("Time").setValue(Currentime());
}
.
whileループが最初に実行され、次に~10 Firebase呼び出しが終了します。次の非同期呼び出しを呼び出す前に非同期(Firebase)呼び出しを1秒間実行するようにタイムアウトを追加する方法はありますか?
解決
Javaの例の例'llは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