英文:
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?
英文:
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?
答案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()) {
...
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论