質問

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);
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top