通过连接 iOS 中的表从 Firebase 获取数据
Fetch data from Firebase by joining tables in iOS
我正在尝试从两个不同的 Firebase table 中获取数据。这是 table 的结构:
Post {
1{
pImages{
i1:true
i2:true
}
}
2{
pImages{
i3:true
}
}
}
Images{
i1{
iUrl : ....
pId : 1
}
i2{
iUrl :...
pId : 1
}
i3{
iUrl:....
pId : 2
}
}
我需要检索对应于id = 1的post的图像。以下是我检索图像的实现:
func retrieveImagesForPost(postId: String,completion: (result: AnyObject?, error: NSError?)->()){
var imgArray:[Image]=[]
let postsRef = self.ref.child("post")
let imagesRef = self.ref.child("image")
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
for item in snapshot.children{
imagesRef.child(item.key).observeSingleEventOfType(.Value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
})
}
print(snapshot.key)
print("called")
completion(result:imgArray, error:nil)
})
}
但是,问题是我无法将 imgArray
中的所有图像发送到 completion handler
。以下是使用 post id ==1.
调用 retrieveImagesForPost
的输出
pImages
called
<TestProject.Image: 0x7f9551e82000>
<TestProject.Image: 0x7f955466a150>
在调用 completion handler
后检索图像。我尝试了 dispatch groups
和 semaphores
方法,如下面 中所述。但结果还是一样。如何让 completion handler
等待从 Firebase 获取所有图像?
保留一个计数器,随着每个图像的加载,该计数器会增加。一旦计数器达到 snapshot.children
列表的长度,您就完成了并调用您的完成处理程序。
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var counter = 0
for item in snapshot.children{
imagesRef.child(item.key).observeSingleEventOfType(.Value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
counter = counter + 1
if (counter == snapshot.childrenCount) {
completion(result:imgArray, error:nil)
}
})
}
})
您可能应该在上面添加一些错误处理,但总的来说,这种方法已经过尝试和测试。
这个问题的另一个答案是使用 GCD 的 DispatchGroup
。
首先你需要用DispatchGroup
创建一个调度组。在这种情况下,您需要用 enter()
手动告诉小组何时开始工作,以及何时用 leave()
完成工作。然后调度组的 notify(queue:execute:)
将在主队列上执行完成处理程序。
小心!进入和离开的数量必须平衡,否则永远不会调用调度组的notify。
let dispatchGroup = DispatchGroup()
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.value, withBlock: { (snapshot) in
for item in snapshot.children{
dispatchGroup.enter()
imagesRef.child(item.key).observeSingleEventOfType(.value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
dispatchGroup.leave()
})
}
})
dispatchGroup.notify(queue: DispatchQueue.main, execute: {
completion(result: imgArray)
})
我正在尝试从两个不同的 Firebase table 中获取数据。这是 table 的结构:
Post {
1{
pImages{
i1:true
i2:true
}
}
2{
pImages{
i3:true
}
}
}
Images{
i1{
iUrl : ....
pId : 1
}
i2{
iUrl :...
pId : 1
}
i3{
iUrl:....
pId : 2
}
}
我需要检索对应于id = 1的post的图像。以下是我检索图像的实现:
func retrieveImagesForPost(postId: String,completion: (result: AnyObject?, error: NSError?)->()){
var imgArray:[Image]=[]
let postsRef = self.ref.child("post")
let imagesRef = self.ref.child("image")
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
for item in snapshot.children{
imagesRef.child(item.key).observeSingleEventOfType(.Value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
})
}
print(snapshot.key)
print("called")
completion(result:imgArray, error:nil)
})
}
但是,问题是我无法将 imgArray
中的所有图像发送到 completion handler
。以下是使用 post id ==1.
retrieveImagesForPost
的输出
pImages
called
<TestProject.Image: 0x7f9551e82000>
<TestProject.Image: 0x7f955466a150>
在调用 completion handler
后检索图像。我尝试了 dispatch groups
和 semaphores
方法,如下面 completion handler
等待从 Firebase 获取所有图像?
保留一个计数器,随着每个图像的加载,该计数器会增加。一旦计数器达到 snapshot.children
列表的长度,您就完成了并调用您的完成处理程序。
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
var counter = 0
for item in snapshot.children{
imagesRef.child(item.key).observeSingleEventOfType(.Value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
counter = counter + 1
if (counter == snapshot.childrenCount) {
completion(result:imgArray, error:nil)
}
})
}
})
您可能应该在上面添加一些错误处理,但总的来说,这种方法已经过尝试和测试。
这个问题的另一个答案是使用 GCD 的 DispatchGroup
。
首先你需要用DispatchGroup
创建一个调度组。在这种情况下,您需要用 enter()
手动告诉小组何时开始工作,以及何时用 leave()
完成工作。然后调度组的 notify(queue:execute:)
将在主队列上执行完成处理程序。
小心!进入和离开的数量必须平衡,否则永远不会调用调度组的notify。
let dispatchGroup = DispatchGroup()
let postImagesRef = postsRef.child(postId).child("pImages");
postImagesRef.observeEventType(FIRDataEventType.value, withBlock: { (snapshot) in
for item in snapshot.children{
dispatchGroup.enter()
imagesRef.child(item.key).observeSingleEventOfType(.value, withBlock: { (snap) in
let image = Image(snapshot: snap)
print(image)
imgArray.append(image)
dispatchGroup.leave()
})
}
})
dispatchGroup.notify(queue: DispatchQueue.main, execute: {
completion(result: imgArray)
})