我使用来自FirebaseDatabase的数据填充Recyclerview(而不是FirebaseRecyclerView)。要接收的数据很多,是用户,可以达到数千。当我收到数据时,应用程序会滞后然后崩溃。我知道数据太多,所以工作很辛苦。有没有办法优化它?
我在logcat中有这个错误
The application may be doing too much work on its main thread
我没有留下深刻的印象,所以我寻求可能解决方案的帮助! 谢谢
recyclerView = (RecyclerView) findViewById(R.id.friend_list2);
//recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
final DatabaseReference mPostReference = FirebaseDatabase.getInstance().getReference().child("user-profile");
mPostReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
username = new ArrayList<>();
uid = new ArrayList<String>();
url = new ArrayList<String>();
for (DataSnapshot data : dataSnapshot.getChildren()){
final FriendItem friendItem = data.getValue(FriendItem.class);
//final String friendItem2 = data.getKey();
username.add(friendItem);
//uid.add(friendItem2);
adapter = new FriendHoldAdapter3(getApplicationContext(), username, uid, url);
//adapter.setClickListener(FriendActivity3.this);
recyclerView.setAdapter(adapter);
/**
adapter = new FriendHoldAdapter2(FriendActivity2.this, username, uid);
adapter.setClickListener(FriendActivity2.this);
recyclerView.setAdapter(adapter);
**/
final List<FriendItem> filteredModelList = filter(username, " ");
adapter.setFilter(filteredModelList);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}});
答案 0 :(得分:1)
Firebase按设计执行网络调用异步,但是在主线程上的onDataChange调用中发生任何事情,使用Asynctask将您的工作转移到某个工作线程。