Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

multithreading - Background threading assistance needed

I'm getting the dreaded "the application may be doing too much work on its main thread."

I've read up on background threading, however, I don't know hoe to use it in my existing code.

Please tell me which parts of the code I should post for assistance. Thanks

elow is the HomeActivity.Class that seems to be the problem, once the user logs is the screen just freezes and gives the thread exception

 public void initDrawer() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout_driver);
        navigationView = findViewById(R.id.nav_view_driver);

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home_driver, R.id.nav_history_driver, R.id.nav_update_info_driver)
                .setOpenableLayout(drawer)
                .build();


        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        navigationView.setNavigationItemSelectedListener(this);
    }
    private void loadUser(){
        AsyncTask.execute(() -> FirebaseDatabase.getInstance().getReference(DriversCommon.user_driver_tbl)
                .child(DriversCommon.userID)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        DriversCommon.currentUser = dataSnapshot.getValue(DriverUser.class);


            onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");

            currentUserRef = FirebaseDatabase.getInstance().getReference(DriversCommon.driver_tbl).child("CurrentDriver")
                    .child(DriversCommon.userID);
            onlineRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot1) {
                    currentUserRef.onDisconnect().removeValue();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

                    }
question from:https://stackoverflow.com/questions/65940982/background-threading-assistance-needed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...