私は2つのコレクション、すなわちユーザーと質問を持っています。Firebaseから同期してデータを取り出す方法は?
userIdを使用してログインしたユーザーに基づいて、currQuestion
値をusers
コレクションから取得します。
currQuestion
の値に基づいて、Firebase Questions
コレクションからquestion
というドキュメントを取得する必要があります。
私がユーザID
rootRef.child("0").child("users")
.queryOrderedByChild("userId")
.queryEqualToValue("578ab1a0e9c2389b23a0e870")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
for child in snapshot.children {
self.currQuestion = child.value["currentQuestion"] as! Int
}
print("Current Question is \(self.currQuestion)")
//print(snapshot.value as! Array<AnyObject>)
}, withCancelBlock : { error in
print(error.description)
})
を取得するために、問題
rootRef.child("0").child("questions")
.queryOrderedByChild("id")
.queryEqualToValue(currQuestion)
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
for child in snapshot.children {
print(child.value["question"] as! String)
}
}, withCancelBlock: { error in
print(error.description)
})
を取得するために以下のコードを使用しました。しかし、上記のコードは、非同期で実行されます。私はこの同期またはリスナーを実装する方法を解決するために解決する必要があるので、currQuestion
値が変更されたら質問クエリーを戻すことができますか?
必要な処理を行うには、補完ブロックを使用します。同期しようとしないでください – Paulw11