使用Android处理Firebase参考的最佳方法 [英] Best way to handle Firebase references with Android

查看:134
本文介绍了使用Android处理Firebase参考的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Android应用程序中处理Firebase数据(读取,写入...)时,您需要获取Firebase参考,然后才能处理数据。



<由于Firebase的引用是一个JSON树,所以如果你指向树根,那么你可以随时访问一个孩子,无论深度如何。



问题:什么是从内存和延迟的角度来处理代码中的这个引用的最好的方法?
$ b $ ul
$ root


  • C1


    • C10

    • C11





    • $ C $
  • / li>


$ b 1 /在应用程序的根目录下创建一个静态Firebase引用。 (C11).setValue(...);这个函数的作用在于:

2 /为孩子创建一个新的firebase ref C11

  Firebase ref =新的Firebase(https://your.firebaseio.com/C1/C11); 
ref..setValue(...);



<3> / Hybrid


  Firebase ref =新的Firebase(https://your.firebaseio.com); 
ref.child(C1).child(C11).setValue(...);



<4> / Hybrid 2

  Firebase ref = new Firebase(https://your.firebaseio.com).child(C1).child(C11); 
ref.setValue(...);

性能是否有差异?



也许您可能会提供一些可读性和维护性的建议吗?

解决方案

Firebase查询和引用是轻量级对象。这些繁重的工作是通过Firebase SDK内部的(并由其管理的)类实现的。

由于这个原因,在你提出的任何方法之间的表现。

个人喜好

通常在每个活动中保留一个引用作为成员。

 类MainActivity扩展AppCompatActivity {
Firebase mRef;

如果我有更多的主要列表类型,我将添加这些成员:

 类MainActivity扩展了AppCompatActivity {
Firebase mRef;
Firebase mUsersRef;
Firebase mPostsRef;

...
@Override
保护无效的onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(...);
...
mRef =新的Firebase(https://yours.firebaseio.com);
mUsersRef = mRef.child(users);
mPostsRef = mRef.child(posts);
...
}

很好的独立。

When you deal with Firebase data (read, write...) in an Android app, you need to get the firebase reference to then be able dealing with data.

As the Firebase reference is a JSON tree, if you point the tree root, you can then always access a child, doesn't matter the deepness.

Question : what is the best way from memory, and latency point of view to handle this reference in the code ?

  • Root
    • C1
      • C10
      • C11
    • C2
      • C21

1/ Create a static Firebase ref to the root in the Application.

MyApplication.getFirebaseRootRef().chid(C1).chid(C11).setValue(...); 

2/ Create a new firebase ref for the child C11

Firebase ref = new Firebase("https://your.firebaseio.com/C1/C11");
ref..setValue(...);

3/ Hybrid

Firebase ref = new Firebase("https://your.firebaseio.com");
ref.child(C1).child(C11).setValue(...);

4/ Hybrid 2

Firebase ref = new Firebase("https://your.firebaseio.com").child(C1).child(C11);
ref.setValue(...);

Is there any difference in performance ?

Maybe you may have some advises for readability and maintenance ?

解决方案

Firebase queries and references are lightweight objects. The heavy lifting is done behind the scenes by classes that are internal to (and managed by) the Firebase SDK itself.

Because of this, there will be no significant difference in performance between any of the approaches you proposed.

Personal preference below

I usually keep a reference as a member in each activity.

class MainActivity extends AppCompatActivity {
    Firebase mRef;

If I have more primary list types, I'll add members for those:

class MainActivity extends AppCompatActivity {
    Firebase mRef;
    Firebase mUsersRef;
    Firebase mPostsRef;

    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
        ...
        mRef = new Firebase("https://yours.firebaseio.com");
        mUsersRef = mRef.child("users");
        mPostsRef = mRef.child("posts");
        ...
    }

By putting everything in each activity, those are nicely self-contained.

这篇关于使用Android处理Firebase参考的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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