gpt4 book ai didi

android - 如何在for循环内同步执行firebase函数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:55:32 25 4
gpt4 key购买 nike

我在 for 循环中从 firebase 数据库中检索数据,但问题是 firebase 函数不是同步执行的,我知道 firebase 函数是异步的。有什么解决办法吗?

代码:-

registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
peopleModelList.clear();

for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
peopleModel = new PeopleModel();

peopleHashMap =(HashMap)snapshot.getValue();

peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
peopleModel.setNickname((String)peopleHashMap.get("nickname"));
peopleModel.setChatRoom(snapshot.getKey());

Log.d("kkkk","1st");

if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
{
registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
peopleModel.setRefresh_token((String)dataSnapshot.child("refresh_token").getValue());
peopleModelList.add(peopleModel);
Log.d("kkkk","2nd");
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
Log.d("kkkk","3rd");

}
iCallBackPeople.peopleAct(peopleModelList);
}

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

输出:-

1st
3rd
2nd

但我想要这样的输出

1st
2nd
3rd

注意:-我搜索过,但从未找到任何解决方案!

How to get data from Firebase Database in a loop?

Firebase addListenerForSingleValueEvent excute later in loop

我试过这个但对我不起作用,它是用最后一项填充我的列表!

public void listOfUsers(final ICallBackPeople iCallBackPeople) {
count=0;
//peopleModelList = new ArrayList<>();
sortedMap = new TreeMap<>();
// peopleModelList = new ArrayList<PeopleModel>(sortedMap.values());
registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// peopleModelList.clear();

for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
peopleModel = new PeopleModel();

peopleHashMap =(HashMap)snapshot.getValue();

peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
peopleModel.setNickname((String)peopleHashMap.get("nickname"));
peopleModel.setChatRoom(snapshot.getKey());

//Object chatRoom = snapshot.getKey();
Log.d("kkkk","1st");

if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
{

addItem(count,iCallBackPeople);
count++;
}
Log.d("kkkk","3rd");

}
Log.d("kkkk","end");
//iCallBackPeople.peopleAct(peopleModelList);
}

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

private void addItem(final int index, final ICallBackPeople iCallBackPeople) {
registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
peopleModel.setRefresh_token((String)dataSnapshot.child("refresh_token").getValue());
Log.d("kkkk","2nd");
sortedMap.put(index, peopleModel);
// sortedMap will sort your list by key (in this case, key is integer)


if(sortedMap.size()==2)
{
peopleModelList = new ArrayList<PeopleModel>(sortedMap.values());
iCallBackPeople.peopleAct(peopleModelList);
}

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

我试过了,但没有得到所有项目,只得到快照中最后一个项目的列表

public void listOfUsers(final ICallBackPeople iCallBackPeople) {

peopleModelList = new ArrayList<>();

registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
{
PeopleModel peopleModel;
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
peopleModelList.clear();

for(final DataSnapshot snapshot : dataSnapshot.getChildren())
{
peopleModel = new PeopleModel();

peopleHashMap =(HashMap)snapshot.getValue();

peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
peopleModel.setNickname((String)peopleHashMap.get("nickname"));
peopleModel.setChatRoom(snapshot.getKey());


if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
{
getToken(new CallBackGetToken() {
@Override
public void token(String token) {
peopleModel.setRefresh_token(token);
Toast.makeText(context, token, Toast.LENGTH_SHORT).show();
peopleModelList.add(peopleModel);

//here i am checking my peopleModelList size is equal or not to snapshot
if(peopleModelList.size()==2)
iCallBackPeople.peopleAct(peopleModelList);
}
},peopleModel);
}
}
}

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

private void getToken(final CallBackGetToken callBackGetToken ,PeopleModel peopleModel) {
registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
callBackGetToken.token((String)dataSnapshot.child("refresh_token").getValue());
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

最佳答案

我认为您应该通过一个接口(interface)(创建一个接口(interface)并在此类中实现)。接口(interface)内的方法应该有一个字符串作为参数(它将接收您的 peopleModel.getChatWith() )。

在您的 if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith()))) 中,您应该请求您的界面。

我认为这是您异步问题的解决方案。

关于android - 如何在for循环内同步执行firebase函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com