【问题标题】:Firebase with android 2 nodes with the same database reference, is it possible?Firebase 与具有相同数据库参考的 android 2 节点,有可能吗?
【发布时间】:2018-02-21 17:32:53
【问题描述】:

我试图避免使用 Firebase 自然产生的异步布局。所以我试图在一个 dataonchanged 方法中完成我的方法。

我需要知道这是否可能以及如何引用 2 个节点,如您所见 ,使用一个引用。 iv尝试了类似的东西

dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate" + "clockOutDate");

//和

dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate");
dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockOutDate");

//将文本设置为一个引用而不是两个引用的方法

protected void onStart() {
        super.onStart();
        final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        dataBaseLastIn.addValueEventListener(new ValueEventListener(){

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dateinList.clear();
                dateOutList.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Date date = postSnapshot.getValue(Date.class);
                    String datt = date.getDate().toString().trim();
                    String user = date.getCurrUser().toString().trim();
                    if (DateNew.equals(datt) && user.equals(uid)) {
                        dateinList.add(date);
                    } else {
                        dateinList.remove(date);
                    }

                    DateOut dateOut = postSnapshot.getValue(DateOut.class);
                    String dattOut = dateOut.getDate().toString().trim();
                    String userOut = dateOut.getCurrUser().toString().trim();
                    if (DateNew.equals(dattOut) && userOut.equals(uid)) {
                        dateOutList.add(dateOut);
                    } else {
                        dateOutList.remove(date);
                    }
                }

                newList = dateinList;
                outList = dateOutList;
                try {
                    totalINList();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }

//设置文字

 public void totalINList() throws ParseException {
        for(int i = 0 ; i < newList.size(); i++) {
            timeDuration.add(newList.get(i).getClockTime());
        }
        Date e = newList.get(newList.size() - 1);
        String timeIn = e.getClockTime();
        txtCurrentTime.setText(timeIn);

        for(int i = 0 ; i < outList.size(); i++) {
            timeDurationOut.add(outList.get(i).getTime());
        }
        DateOut f = outList.get(outList.size() - 1);
        String timeOut = f.getTime();
        txtCurrentTimeOut.setText(timeOut);
    }

可能很简单,但我只获得了我声明的最后一个引用的值,这显然是有道理的,但想知道是否有一种方法可以同时引用两个节点

【问题讨论】:

标签: java android firebase firebase-realtime-database


【解决方案1】:

不,您无法使用 Firebase 实现类似的目标。

大多数开发人员的最初反应是尝试“修复”这个asynchronous behavior,我个人建议不要这样做。 Web 是异步的,您越早接受这一点,就能越早了解如何使用现代 Web API 提高工作效率。

【讨论】:

  • 我完全同意,我只是时间不够,所以希望有一个快速修复/(解决方法)。我可以通过将所有数据放在 Fire-base 中的一个节点下来解决它,但这又不是一个好习惯。我将阅读更多关于异步的内容,我知道从长远来看它会对我更有利
  • 为了更好地理解,看看我在post的回答。
  • 感谢您的链接,我重新构建了 ondata 中的代码已更改并将 2 个实体链接到数据库的另一个表中,因此能够以这种方式获得它。感谢您的帮助,非常感谢 :-)
  • 不客气@JamesFerry 如果您认为我对该帖子的回答很有帮助,请考虑投票。谢谢!
猜你喜欢
  • 2019-09-28
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
相关资源
最近更新 更多