该应用程序可能在Andriod的主线程(Firebase数据库)上做了太多工作

时间:2017-12-14 05:55:27

标签: android multithreading arraylist firebase-realtime-database android-recyclerview

  

碎片代码

 ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                list = new ArrayList<>();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                    for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {

                        try {

                            Model listdata = new Model();

                            Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);

                            //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();

                            String path = for_post_details.getPath();
                            String location = for_post_details.getLocation();
                            String caption = for_post_details.getCaption();
                            String username = "" + dataSnapshot1.child("username").getValue();

                            listdata.setPath(path);
                            listdata.setLocation(location);
                            listdata.setCaption(caption);
                            listdata.setUsername(username);

                            list.add(listdata);

                        } catch (Exception ignored) {
                        }
                    }

                }
                Adapter recyclerview = new Adapter(list, getActivity());
                RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutmanager);
                recycler.setItemAnimator(new DefaultItemAnimator());
                recycler.setAdapter(recyclerview);

            }

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

适配器代码

public class Adapter extends RecyclerView.Adapter<Adapter.MyHoder> {

    List<Model> list;
    Context context;

    public Adapter(List<Model> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.recycler_trending,parent,false);
        MyHoder myHoder = new MyHoder(view);
        return myHoder;
    }

    @Override
    public void onBindViewHolder(MyHoder holder, int position) {
        Model mylist = list.get(position);
        holder.caption.setText(mylist.getCaption());
        holder.location.setText(mylist.getLocation());
        holder.username_top.setText(mylist.getUsername());
        holder.username_bottom.setText(mylist.getUsername());
        Glide.with(context)
                .load(mylist.getPath())
                .into(holder.image);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHoder extends RecyclerView.ViewHolder{
        TextView total_likes, caption, location, username_top, username_bottom;
        ImageView image;
        ImageButton like, share;

        public MyHoder(View itemView) {
            super(itemView);
            total_likes = (TextView) itemView.findViewById(R.id.total_likes);
            caption = (TextView) itemView.findViewById(R.id.caption);
            username_top = (TextView) itemView.findViewById(R.id.username_top);
            username_bottom = (TextView) itemView.findViewById(R.id.username_bottom);
            location = (TextView) itemView.findViewById(R.id.location);
            image = (ImageView) itemView.findViewById(R.id.image);
            like = (ImageButton) itemView.findViewById(R.id.like);
            share = (ImageButton) itemView.findViewById(R.id.share);

        }
    }

这是我用来构建像Instagram这样的应用程序的代码。

代码运行正常,但UI速度慢,滚动速度慢,应用程序挂起。

我收到了这条消息。

  

I / Choreographer(1378):跳过65帧!申请可能是   在主线上做了太多工作。

我做错了什么?

  

你知道Instagram使用哪种技术来展示帖子吗?   顺利?

由于

2 个答案:

答案 0 :(得分:1)

runOnUiThread()内运行上述方法,只需将其复制到片段

中的onViewCreated()
    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    list = new ArrayList<>();
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                        for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {

                            try {

                                Model listdata = new Model();

                                Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);

                                //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();

                                String path = for_post_details.getPath();
                                String location = for_post_details.getLocation();
                                String caption = for_post_details.getCaption();
                                String username = "" + dataSnapshot1.child("username").getValue();

                                listdata.setPath(path);
                                listdata.setLocation(location);
                                listdata.setCaption(caption);
                                listdata.setUsername(username);

                                list.add(listdata);

                            } catch (Exception ignored) {
                            }
                        }

                    }
                    Adapter recyclerview = new Adapter(list, getActivity());
                    RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                    recycler.setLayoutManager(layoutmanager);
                    recycler.setAdapter(recyclerview);

                }

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

<强>更新 另一种方法是使用AsyncTask

将此代码复制到onViewCreated()

之外
private class LoadData extends AsyncTask<String, String, String>
{
    final ProgressDialog pDialog = new ProgressDialog(getActivity());

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        pDialog.setMessage("Loading please wait..");
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setIndeterminate(true);

        pDialog.show();
    }

    @Override
    protected String doInBackground(String... strings)
    {
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                list = new ArrayList<>();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                    for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) {

                        try {

                            Model listdata = new Model();

                            Model for_post_details = dataSnapshot2.child("info").getValue(Model.class);

                            //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show();

                            String path = for_post_details.getPath();
                            String location = for_post_details.getLocation();
                            String caption = for_post_details.getCaption();
                            String username = "" + dataSnapshot1.child("username").getValue();

                            listdata.setPath(path);
                            listdata.setLocation(location);
                            listdata.setCaption(caption);
                            listdata.setUsername(username);

                            list.add(listdata);

                        } catch (Exception ignored) {
                        }
                    }

                }

            }

            @Override
            public void onCancelled(DatabaseError error) {
            Log.d("anyError", error.toString());
            }
        });

    }

    @Override
    protected void onPostExecute(String lengthOfFile)
    {

               Adapter recyclerview = new Adapter(list, getActivity());
                RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity());
                recycler.setLayoutManager(layoutmanager);
                recycler.setAdapter(recyclerview);

        if ((pDialog != null)  && (pDialog.isShowing()))
        {
            pDialog.dismiss();
        }
    }
}

并在onViewCreated()内,只需致电new LoadData().execute();

答案 1 :(得分:0)

addValueEventListener在数据更改时不断调用创建ui卡住尝试使用addListenerForSingleValueEvent,它只会读取一次数据并添加一个计时器任务来定期更新应用程序的数据,它只会在计时器完成时偶尔更新数据不会卡住你的ui