我在onCreate()中有一些代码冻结了UI线程。我已经尝试过以我能找到的每种不同方式使其在后台执行。我唯一能想到的是我在主UI线程中有其他firebase事件监听器,所以即使代码在后台执行,其他监听器也会在后台更新时冻结活动。或者我的代码可能没有真正到达背景?有没有人有任何建议?
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//tried it here
}
});
////////
Thread thread = new Thread() {
@Override
public void run() {
//tried it here
}
});
thread.start();
/////
Runnable runnable = new Runnable() {
@Override
public void run() {
//tried it here
}
};
AsyncTask.execute(runnable);
///这是代码。我此刻正在尝试这种方式。
Thread custListLoadThread = new Thread(runnable);
custListLoadThread.start();
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
mSelectedPlayerRef.child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);
final ArrayList<String> mEnteredPlayers = new ArrayList<>();
mEntriesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot entries: dataSnapshot.getChildren()){
Player player = entries.getValue(Player.class);
mEnteredPlayers.add(player.getUsername());
Player draftedPlayer = entries.getValue(Player.class);
mEntriesRef.child(draftedPlayer.getUsername()).child("VS")
.child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);
}
for(String enteredPlayer: mEnteredPlayers){
mSelectedPlayerRef.child(enteredPlayer).setValue(mEmptyResultsItems);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
所有这些都冻结了UI线程。
答案 0 :(得分:0)
我还没有对此进行测试,但这就是我要做的: `
final ArrayList<String> mEnteredPlayers = new ArrayList<>();
mEntriesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
new Thread(new Runnable() {
for(DataSnapshot entries: dataSnapshot.getChildren()){
Player player = entries.getValue(Player.class);
mEnteredPlayers.add(player.getUsername());
Player draftedPlayer = entries.getValue(Player.class);
mEntriesRef.child(draftedPlayer.getUsername()).child("VS").child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);
}
for(String enteredPlayer: mEnteredPlayers){
mSelectedPlayerRef.child(enteredPlayer).setValue(mEmptyResultsItems);
}
}).start();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
`
答案 1 :(得分:0)
尝试删除线程并在OnCreate方法中运行代码....像这样
@覆盖 protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
mSelectedPlayerRef.child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);
final ArrayList<String> mEnteredPlayers = new ArrayList<>();
mEntriesRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot entries: dataSnapshot.getChildren()){
Player player = entries.getValue(Player.class);
mEnteredPlayers.add(player.getUsername());
Player draftedPlayer = entries.getValue(Player.class);
mEntriesRef.child(draftedPlayer.getUsername()).child("VS")
.child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);
}
for(String enteredPlayer: mEnteredPlayers){
mSelectedPlayerRef.child(enteredPlayer).setValue(mEmptyResultsItems);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}
答案 2 :(得分:0)
使用处理程序:
protected static Handler sHandler = new Handler();
使用您按原样创建的Runnable。
然后代替:
Thread custListLoadThread = new Thread(runnable);
custListLoadThread.start();
使用:
sHandler.post(runnable);