0

這是我的服務類,呼籲所有聽衆火力地堡實施凍結的應用程序同時下載數據

public class DBService extends Service { 

private static final String TAG = DBService.class.getName(); 
private DatabaseReference reference; 

private static final String FIREBASE_EMAIL = "[email protected]"; 
private static final String FIREBASE_PASSWORD = "xxxxxx"; 

@Override 
public void onCreate() { 
    super.onCreate(); 

    FirebaseDatabase database = FirebaseDatabase.getInstance(); 
    reference = database.getReference(); 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    FirebaseAuth auth = ((StartApplication) getApplication()).getAuth(); 
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 

    if (user == null) { 

     String email = ObjectGraph.getEmployeeProfile().getEmail(); 
     String password = ObjectGraph.getEmployeeProfile().getMobile_no(); 
     if (password != null && !password.trim().isEmpty()) { 
      if (email == null || email.trim().isEmpty()) { 
       email = password + FIREBASE_EMAIL; 
      } 
      signIn(auth, email, FIREBASE_PASSWORD); 
     } 
    } else { 
     addListeners(); 
    } 
    return START_STICKY; 

} 

@Override 
public IBinder onBind(Intent intent) { 
    addListeners(); 
    return null; 
} 

private void signIn(final FirebaseAuth auth, final String email, final String password) { 

    Log.i(TAG, "Login"); 
    auth.signInWithEmailAndPassword(email, password) 
      .addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 

        if (task.isSuccessful()) { 
         boolean isResetTimeStamp = true; 
         setTimeStamp(isResetTimeStamp); 
         addListeners(); 
        } else { 
         register(auth, email, password); 
        } 
       } 
      }); 

    FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 
      } else { 
       Log.e(TAG, "onAuthStateChanged:signed_out"); 
      } 
     } 
    }; 

    auth.addAuthStateListener(authListener); 
} 

private void addListeners() { 
    EmployeeProfile profile = ObjectGraph.getEmployeeProfile(); 
    if (profile != null && profile.getMobile_no() != null && !profile.getMobile_no().trim().isEmpty()) { 
     reference.child(AppConstants.WORKINDIA_USERS_LAST_TIME).child(profile.getMobile_no().trim()).addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       Map<String, Object> child = (Map<String, Object>) dataSnapshot.getValue(); 
       Log.e(TAG, "DATA " + child); 
       if (child == null) { 

       /*Query Listener Other listener*/ 
        Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE).limitToFirst(1000);//.orderByChild("timestamp"); 
        recentPostsQuery.addValueEventListener(jobBulKDownloadListener); 

       } else { 

        long lastSyncTime = (Long) child.get(AppConstants.TIMESTAMP); 
        Log.e(TAG, "DATA " + lastSyncTime); 

       /*Query Listener Other listener*/ 
        Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE) 
          .orderByChild(AppConstants.TIMESTAMP) 
          .startAt(lastSyncTime) 
          .limitToFirst(1000);//.orderByChild("timestamp"); 
        recentPostsQuery.addValueEventListener(jobBulKDownloadListener); 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.e(TAG, databaseError.getMessage(), databaseError.toException()); 
      } 
     }); 
    } 
} 



private void register(final FirebaseAuth auth, final String email, final String password) { 
    Log.e(TAG, "register"); 
    auth.createUserWithEmailAndPassword(email, password) 
      .addOnCompleteListener(new OnCompleteListener<AuthResult>() { 

       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        if (task.isSuccessful()) { 
         Log.e(TAG, "register true"); 
         signIn(auth, email, password); 
        } else { 
         Log.e(TAG, "register fail"); 
        } 
       } 

      }); 
} 


ValueEventListener jobBulKDownloadListener = new ValueEventListener() { 
    @Override 
    public void onDataChange(final DataSnapshot dataSnapshot) { 

     Toast.makeText(getApplicationContext(), "jobBulKDownloadListener+ onDataChange", Toast.LENGTH_SHORT).show(); 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        if (dataSnapshot != null) { 
         long time = System.currentTimeMillis(); 
         Log.d(TAG, "Start Process : " + (System.currentTimeMillis() - time)/1000 + " Seconds"); 
         List<Job> jobs = new ArrayList<Job>(); 
         for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 

          WrapperJob job1 = snapshot.getValue(WrapperJob.class); 
          Job job = job1.getData(); 
          jobs.add(job); 
         } 


         if (jobs.size() > 0) { 
          parseJobs(jobs); 
         } 
         Log.d(TAG, "After Process : " + (System.currentTimeMillis() - time)/1000 + " Seconds"); 
        } 
       } catch (Exception e) { 
        Log.e(TAG, e.getMessage(), e); 
        Crashlytics.logException(e); 
       } 
      } 
     }).start(); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     Log.e(TAG, databaseError.getMessage(), databaseError.toException()); 
    } 
}; 


private void parseJobs(List<Job> jobs) { 

    /* Job Operations*/ 
} 

} 

爲什麼反而越來越吊死?我已將幾乎所有內容都保留在後臺線程上

+0

在問題發生之前處理得到多少?你看到'jobBulKDownloadListener'的「Start Process」和「After Process」日誌消息嗎? –

+0

Firebase數據庫已經在後臺線程上完成了大部分工作(網絡,磁盤I/O等)。你不必爲此做任何事情。主線程上所做的就是調用你的處理程序。你可以放大到哪個確切的電話被阻止?如果在該調用中存在回調:是否從回調中刪除自己的代碼,請刪除問題? –

+0

有些時間花了20到22秒,但我通過刪除所有進程並打印日誌來檢查,然後掛起 –

回答

0

這可能不是問題,但在jobBulKDownloadListener中,在主線程而不是工作線程上運行DataSnapshot可能更安全。你可以像這樣重構它:

ValueEventListener jobBulKDownloadListener = new ValueEventListener() { 
    @Override 
    public void onDataChange(final DataSnapshot dataSnapshot) { 
     Toast.makeText(getApplicationContext(), "jobBulKDownloadListener+ onDataChange", Toast.LENGTH_SHORT).show(); 

     if (dataSnapshot != null) { 
      final List<Job> jobs = new ArrayList<Job>(); 
      for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 

       WrapperJob job1 = snapshot.getValue(WrapperJob.class); 
       Job job = job1.getData(); 
       jobs.add(job); 
      } 

      if (jobs.size() > 0) { 
       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          long time = System.currentTimeMillis(); 
          Log.d(TAG, "Start Process : " + (System.currentTimeMillis() - time)/1000 + " Seconds"); 
          parseJobs(jobs); 
          Log.d(TAG, "After Process : " + (System.currentTimeMillis() - time)/1000 + " Seconds"); 
         } catch (Exception e) { 
          Log.e(TAG, e.getMessage(), e); 
          Crashlytics.logException(e); 
         } 
        } 
       }).start(); 
      } 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     Log.e(TAG, databaseError.getMessage(), databaseError.toException()); 
    } 
}; 
+0

當我在主要操作時,它凍結非常糟糕,這就是爲什麼我將它移動到線程部分。但我再次嘗試不工作。 –