更有效的方式来检索Firebase数据? [英] More efficient way to retrieve Firebase Data?

查看:156
本文介绍了更有效的方式来检索Firebase数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组分层的数据,我想从Firebase中检索信息。以下是我的数据:
<但是,我的问题是这样的:在看数据是如何构造的,当我想抓取名称对象id 参与者,我必须执行以下代码:

  var attendeesReference = self.eventsReference.childByAppendingPath((child as String )+/ attendees)

attendeesReference.observeEventType(FEventType.ChildAdded,withBlock:{(snapshot) - >无效

//获取名称/对象ID -
让name = snapshot.value.objectForKey(name)as?String
let objectID = snapshot.value.objectForKey(objectID)as?String
if snapshot!= nil {
println(Name:\(名称)对象ID:\(objectID))
completion(result:true,name:name,objectID:objectID)
}

}){(error) - >无效
println(error.description)
}
}

现在,每个与会者的孩子都会经历一个一个的抓取名称和对象ID。当函数完成时,我将每个值存储到一个字典中。一旦这样做,这个函数被多次调用,而且可能非常慢,尤其是在数据库往返数次的时候。有没有更有效的方法来做到这一点?我已经试图查看 FEeventType.Value ,但似乎返回参与者子项中的所有内容,当我真的要保存的每个参与者名称 objectID 某种字典。任何帮助,将不胜感激。感谢!

解决方案

Firebase的黄金法则之一就是只有在您始终要检索所有数据它。这个规则的原因是Firebase总是返回一个完整的节点。您不能部分检索该节点中的某些数据,而不是其他数据。



关于构建数据的Firebase指南,对此进行了说明:


因为我们可以将数据嵌套深度达32层,所以很容易认为这应该是默认的结构。但是,当我们在数据库中的某个位置获取数据时,我们还检索了它的所有子节点。因此,在实践中,最好尽可能保持平坦,就像构建SQL表一样。


您应该阅读整个文档的一部分,因为它包含了一些很好的例子。

在你的情况下,你需要修改你的数据结构来分隔事件参与者和事件元数据:

  events 
-JFSDFHdsf89498432
eventCreator:Stephen
eventCreatorId:1764137
-JOeDFJHFDSHJ14312
eventCreator:puf
eventCreatorId:892312
event_attendees
-JFSDFHdsf89498432
-JSAJKAS75478
name:Johnny Appleseed
-JSAJKAS75412
名称:use1871869
-JOeDFJHFDSHJ14312
-JaAasdhj1382
名称:Frank van Puffelen
-Jo1asd138921
名称: use1871869

通过这种方式,您可以检索事件元数据而不需要检索at投标,反之亦然。

I have a hierarchical set of data that I want to retrieve information from Firebase. Below is how my data looks:

However, my issue is this: Upon looking at how the data is structured, when I want to grab the name or object id of an attendee, I have to perform the following code:

func getAttendees(child: NSString, completion: (result: Bool, name: String?, objectID: String?) -> Void){
    var attendeesReference = self.eventsReference.childByAppendingPath((child as String) + "/attendees")

    attendeesReference.observeEventType(FEventType.ChildAdded, withBlock: { (snapshot) -> Void in

        //Get the name/object ID of the attendee one by one--inefficient?
        let name = snapshot.value.objectForKey("name") as? String
        let objectID = snapshot.value.objectForKey("objectID") as? String
        if snapshot != nil {
            println("Name: \(name) Object ID: \(objectID)")
            completion(result: true, name: name, objectID: objectID)
        }

        }) { (error) -> Void in
            println(error.description)
    }   
}

Now this goes through every attendee child and grabs the name and object id one by one. When the function completes, I store each value into a dictionary. Upon doing so, this function is called multiple times and can be very slow especially when going to/from a database so many times. Is there a more efficient way to do this? I have tried to look into FEeventType.Value but that seems to return everything within the attendees child, when all I really want are the name and objectID of each attendee stored into some sort of dictionary. Any help would be appreciated. Thanks!

解决方案

One of the golden rules of Firebase is to only nest your data when you always want to retrieve all of it. The reason for this rule is that Firebase always returns a complete node. You cannot partially retrieve some of the data in that node and not other data.

The Firebase guide on structuring data, says this about it:

Because we can nest data up to 32 levels deep, it's tempting to think that this should be the default structure. However, when we fetch data at a location in our database, we also retrieve all of its child nodes. Therefore, in practice, it's best to keep things as flat as possible, just as one would structure SQL tables.

You should really read that entire section of the docs, since it contains some pretty good examples.

In your case, you'll need to modify your data structure to separate the event attendees from the event metadata:

events
  -JFSDFHdsf89498432
    eventCreator: "Stephen"
    eventCreatorId: 1764137
  -JOeDFJHFDSHJ14312
    eventCreator: "puf"
    eventCreatorId: 892312
event_attendees
  -JFSDFHdsf89498432
    -JSAJKAS75478
      name: "Johnny Appleseed"
    -JSAJKAS75412
      name: "use1871869"
  -JOeDFJHFDSHJ14312
    -JaAasdhj1382
      name: "Frank van Puffelen"
    -Jo1asd138921
      name: "use1871869"

This way, you can retrieve the event metadata without retrieving the attendees and vice versa.

这篇关于更有效的方式来检索Firebase数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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