从Firebase中的引用正确使用hasChild()和forEach()? [英] Proper use of hasChild() and forEach() from a reference in Firebase?

查看:85
本文介绍了从Firebase中的引用正确使用hasChild()和forEach()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了我的理解和SQL背景知识,我并没有完全理解hasChild()和forEach()的用法。他们觉得他们属于引用而不是快照。
$ b $(我将在后面的讨论中使用hasChild(),但概念是可以互换的。)

以下是我的推理:


  1. 我有一个Firebase路径,我想查看用户(Fred)是否存在

  2. li>我获得一个参考: var users = new Firebase('appname / users')

但我无法确定一个孩子是否从这里存在。所以这里是我现在的:
$ b $ pre $ users.child('Fred')。once('value',function(snapshot ){
/ **如果Fred不存在,**会永远等待* /
});

但这并不奏效。所以现在我必须得到这样的用户的价值(这感觉有点反直觉,因为我不感兴趣的用户):

  var users = new Firebase('http://.../appname/users'); 
users.once('value',function(snapshot){
snapshot.childExists('Fred',function(exists){
/ *在这里做某事* /
} );
});

我不会想到通过获取 appname / users ,基于文档,但是如果我只是想确定关键字'Fred'是否存在,这感觉就像是一个难看的代码。



我希望看到如下内容:

  var users = new Firebase('http://.../appname /用户); 
users.hasChild('Fred',function(exists [,snapshotOfFred]){
/ *在这里做某事* /
});

有更好的方法来使用forEach / hasChild吗?我错过了任何重要的逻辑考虑吗?

.once()会触发你的回调,不管Fred是否已经存在。所以你可以这样做:

$ $ p $ users.child('Fred')。once('value',function(snapshot){
if(snapshot.val()=== null){
/ *没有用户'Fred'!* /
} else {
/ * User'Fred'存在。
}
});您的第二个代码片段,实际上会产生很大的开销,因为Firebase会从Firebase获取整个用户位置,如果您有1000位用户,则可能会相当大!所以第一个代码段肯定是要走的路。



hasChild和forEach在DataSnapshot而不是Firebase上的原因是它们可以是同步的。在我们早期的API测试中,我们在Firebase引用中混合了同步和异步方法,但是我们发现这是人们的一个重要绊脚石(如果人们看到hasChild方法,他们会期望它返回一个布尔值立即)。所以我们创建.on()和.once()是Firebase上唯一的异步回调方法。 Firebase引用中的其他所有内容都是同步的(尽管我们有时会提供可选的异步完成回调),DataSnapshot上的所有内容都是100%同步的。



所以我们的目标是Firebase的异步性更易于人们理解和使用!但也许我们还没有成功100%。 :-)在规划未来的API调整时,我们会考虑您的反馈意见。谢谢!


With my limited understanding, and SQL background, I'm not quite groking the usage of hasChild() and forEach(); they feel like they belong on the reference and not the snapshot.

(I'll use hasChild() for the rest of the discussion, but the concepts are interchangeable.)

Here's my reasoning:

  1. I have a Firebase path called for a users table, say appname/users
  2. I want to see if a user (Fred) exists
  3. I obtain a reference: var users = new Firebase('appname/users')

But I can't determine if a child exists from here. So here is what I have now:

users.child('Fred').once('value', function(snapshot) {
   /** waits around forever if Fred doesn't exist */
});

But that doesn't quite work. So now I must get the value of users (which feels a bit counter-intuitive, since I'm not interested in users) with something like this:

var users = new Firebase('http://.../appname/users');
users.once('value', function(snapshot) { 
      snapshot.childExists('Fred', function(exists) { 
         /* do something here*/ 
      }); 
   });

I don't imagine I incur a large overhead by fetching appname/users, based on the docs, but this feels like an unsightly bit of code if I just want to determine if the key 'Fred' exists.

I'd like to see something like:

var users = new Firebase('http://.../appname/users');
users.hasChild('Fred', function(exists[, snapshotOfFred]) { 
   /* do something here*/ 
});

Is there a better approach to using forEach/hasChild? Have I missed any important logical considerations here?

解决方案

You were actually on the right track with your first code snippet. .once() will trigger your callback with a snapshot, regardless of whether Fred already exists or not. So you can just do:

users.child('Fred').once('value', function(snapshot) {
   if (snapshot.val() === null) {
       /* There is no user 'Fred'! */
   } else {
       /* User 'Fred' exists.
   }
});

Your second code snippet would actually incur a large overhead, since Firebase would fetch the entire 'users' location from Firebase, which could be quite large if you have 1000s of users! So the first code snippet is definitely the way to go.

The reason that hasChild and forEach are on the DataSnapshot instead of on Firebase is so that they can be synchronous. In our early API testing, we had a mix of synchronous and asynchronous methods available on the Firebase reference, but we found that this was a significant stumbling block for folks (if people see a hasChild method, they'll expect it to return a boolean immediately). So we created .on() and .once() to be the one and only asynchronous callback methods on Firebase. Everything else on a Firebase reference is synchronous (though we sometimes provide optional asynchronous completion callbacks), and everything on a DataSnapshot is 100% synchronous.

So our goal was to make the asynchronous nature of Firebase easier for people to understand and use! But perhaps we haven't succeeded 100%. :-) We'll take your feedback into account when planning future API tweaks. Thanks!

这篇关于从Firebase中的引用正确使用hasChild()和forEach()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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