通过在iOS中连接表格从Firebase获取数据 [英] Fetch data from Firebase by joining tables in iOS

查看:113
本文介绍了通过在iOS中连接表格从Firebase获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从两个不同的Firebase表中获取数据。这里是表格的结构:

pre $
$ {
pImages {
i1 :true
i2:true
}
}
2 {
pImages {
i3:true

}


$ {
i1 {
iUrl:....
pId:1
}
i2 {
iUrl:...
pId:1
}
i3 {
iUrl:....
pId:2
}
}

我需要检索与id = 1的post对应的图像。图片:
$ b $ pre $ 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(pos TID).child( pImages);
postImagesRef.observeEventType(FIRDataEventType.Value,withBlock:{(snapshot)in
for 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 中的所有图像到 code> $ b>能够发送到完成处理程序。以下是调用 retrieveImagesForPost 的帖子id = 1的输出。

  pImages 

< TestProject.Image:0x7f955466a150>

完成处理程序叫做。我尝试了调度组信号量方法,如下所示帖子。但结果还是一样的。我怎样才能让完成处理程序等待从Firebase获取所有图片?

解决方案保持一个计数器,你增加每个图像加载。一旦计数器达到 snapshot.children 列表的长度,就完成并调用完成处理程序。

  let postImagesRef = postsRef.child(postId).child(pImages); 
postImagesRef.observeEventType(FIRDataEventType.Value,withBlock:{(snapshot)in
var counter = 0
用于snapshot.children中的物品{
imagesRef.child(item.key) .bserveSingleEventOfType(.Value,withBlock:{(snap)in
let image = Image(snapshot:snap)
print(image)
imgArray.append(image)
counter =计数器+ 1
if(counter == snapshot.childrenCount){
completion(result:imgArray,error:nil)
}
})
}
})

您应该在上面添加一些错误处理,但总的来说,并经过测试。

I am trying to fetch data from two different Firebase tables. Here is the structure of 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
         }
 }

I need to retrieve images corresponding to post with id = 1. The following is my implementation to retrieve images:

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

But, the problem is I am not able to get all images in imgArray to be able to send to completion handler. Below is the output of calling retrieveImagesForPost with post id ==1.

pImages
called
<TestProject.Image: 0x7f9551e82000>
<TestProject.Image: 0x7f955466a150>

The images are retrieved after the completion handler is called. I tried the dispatch groups and the semaphores approach as described in the following post. But the results are still the same. How can I make completion handler to wait for all images to be fetched from Firebase?

解决方案

Keep a counter that you increase as each image is loaded. Once the counter reaches the length of the snapshot.children list, you're done and call your completion handler.

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

You should probably add some error handling in the above, but in general this approach is tried and tested.

这篇关于通过在iOS中连接表格从Firebase获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆