Firebase:如何确定不同父项中的重复项

huangapple go评论70阅读模式
英文:

Firebase: How to determine duplicate in different parents

问题

我有以下代码来在“Admin” Firebase 中拒绝重复项:

public static void checkForDuplicateTweet()
{
  Query adminQuery = dbRefAdminFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);
  Query feedQuery = dbRefFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);

  adminQuery.addValueEventListener(new ValueEventListener()
  {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    {
      if (dataSnapshot.exists())
      {
        boolean isFirst = true;

        for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
        {
          TweetData tweetData = postSnapshot.getValue(TweetData.class);

          if (isFirst)
          {
            isFirst = false;
          }
          else
          {
            removeTweetFromFirebase(Objects.requireNonNull(tweetData).tweetKey);
          }
        }
      }
    }

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

我如何检查不同的父级,以查看是否在该父级下存在具有以下结构的 tweetStatusID?

Firebase:如何确定不同父项中的重复项

英文:

I have the following code to reject duplicates in the "Admin" Firebase:

  public static void checkForDuplicateTweet()
  {
    Query adminQuery = dbRefAdminFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);
    Query feedQuery = dbRefFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);

    adminQuery.addValueEventListener(new ValueEventListener()
    {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
      {
        if (dataSnapshot.exists())
        {
          boolean isFirst = true;

          for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
          {
            TweetData tweetData = postSnapshot.getValue(TweetData.class);

            if (isFirst)
            {
              isFirst = false;
            }
            else
            {
              removeTweetFromFirebase(Objects.requireNonNull(tweetData).tweetKey);
            }
          }
        }
      }

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

How do I check a different parent to see if the tweetStatusID exists under that parent with the following structure?

Firebase:如何确定不同父项中的重复项

答案1

得分: 0

你已经完成了一半。要检查该值是否存在于两个父级下,您需要在每个父级上运行查询。因此,只需在dbRefFeed上运行查询,一旦确定该值存在于dbRefAdminFeed下方。

Query adminQuery = dbRefAdminFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);
Query feedQuery = dbRefFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);

adminQuery.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot adminSnapshot) {
    if (adminSnapshot.exists()) {
      feedQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot feedSnapshot) {
          if (feedSnapshot.exists()) {
              ...
          }
        }
      });
    }
  }
});
英文:

You're halfway there. To check if the value exists under both parents, you have to run a query on each parent. So just run the query on dbRefFeed, once you've determine that the value exists under dbRefAdminFeed.

Query adminQuery = dbRefAdminFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);
Query feedQuery = dbRefFeed.orderByChild("tweetStatusID").equalTo(TweetData.tweetStatusID);

adminQuery.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot adminSnapshot) {
    if (adminSnapshot.exists()) {
      feedQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot feedSnapshot) {
          if (feedSnapshot.exists()) {
              ...
          }
        }
    }
  }

huangapple
  • 本文由 发表于 2020年9月25日 21:33:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64065190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定