我如何指向随机密钥(firebase用户ID) [英] how can i point to the random key( firebase user ID)

查看:45
本文介绍了我如何指向随机密钥(firebase用户ID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据创建日期和时间从 Firebase 中检索数据我没有找到其他方法来代替使用 orderByChild("Create")创建每个用户的孩子来保存创建日期和时间排序,但是每个用户都保存了一个随机ID,该怎么办?我指的是按以下子项排序:

I want to retrieve data from Firebase according to creation date and time I haven't found any other method instead of creating a child of every user to save creation date and time sort by using orderByChild("Create") but every user is saved with a random id how can I point to sort by the child of:

DatabaseReference userref = FirebaseDatabase.getInstance().getReference().child("Connection").child("Admin");
        userref.orderByChild("Create");

        userref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                        Users users = userSnapshot.getValue(Users.class);
                        adminsList.add(users);

                    }
                    mAdapter = new Adapter_All_Admins(adminsList);
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
                    mRecyclerView.setAdapter(mAdapter);


                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


推荐答案

您的代码中存在多个问题.

There are more than a single problem in your code.

我没有找到其他方法来代替通过使用orderByChild("Create")创建每个用户的孩子来保存创建日期和时间的方法

I haven't found any other method instead of creating a child of every user to save creation date and time sort by using orderByChild("Create")

这是为每个用户创建一个单独节点的推荐方法.第一个问题是Firebase实时数据库查询是不可变的,这意味着您无法更改现有查询的属性.如果通过调用 .orderByChild("Create")方法更改值,则该方法将成为新查询.这与 String 类.因此,要解决此问题,请链接所有方法调用,并将它们存储在单个 Query 对象中:

That's the recommended approach to create a separate node for every user. First problem is that Firebase realtime database queries are immutable, which means that you cannot change the properties of an existing query. If you change the value by calling .orderByChild("Create") method, it becomes a new query. That's the exact same behaviour as in case of the String class. So to solve this, please chain all method calls and store them in a single Query object:

Query userref = FirebaseDatabase.getInstance().getReference()
    .child("Connection")
    .child("Admin")
    .orderByChild("Create");

或者您可以像这样创建一个新的 Query 对象:

Or you can create a new Query object like this:

Query createQuery = userref.orderByChild("Create");
createQuery.addValueEventListener(/* ... */);

第二个问题,要获得相关结果,请注意,时间戳记应该存储为字符串:

Second problem, to have relevant results, please note that the timestamp should not be stored as a String:

创建:"2019/09/11 02.32:54"

因为这种情况下的订单是按字典顺序.因此,您绝对应该将它们存储为 ServerValue.TIMESTAMP ,如以下帖子中我的回答所述:

Because the order in this case would be lexicographically. So you definitely should store them as a ServerValue.TIMESTAMP, as explained in my answer from the following post:

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