2016-03-16 115 views
0

我在將Google Drive API集成到我的Android應用程序時遇到了一些問題。基本上,我試圖創建一個文件夾來存儲我的錯誤日誌。問題是,偶爾線程會運行,並且該文件夾將不會在文件需要創建時創建,所以應用程序崩潰。我正在考慮如何避免這種情況,並嘗試了一些方法,包括在文件夾回調中進行文件夾分配,但都無濟於事。我有時很幸運(特別是在調試時,不幸的是),並且文件保存好,但我顯然需要解決併發問題。請讓我知道是否需要更多的代碼上下文。Android上Google Drive API的併發問題

protected void onPause() { 
    // TODO Find the right place for this 
    if (!mDriveLogList.isEmpty()) { 
     // Perform I/O off the UI thread. 
     // TODO Fix to use RxJava 
     new Thread() { 
      @Override 
      public void run() { 
       Timber.i("Starting File Creation"); 
       // write content to DriveContents 
       if (mDriveContents == null) { 
        Timber.e("Drive Contents Were Null"); 

       } else { 
        OutputStream outputStream = mDriveContents.getOutputStream(); 
        Writer writer = new OutputStreamWriter(outputStream); 
        //TODO Clean/Refactor 
        try { 
         writer.write("Status Log For: " + DateHelper.getDate() + "\n"); 
         for (String logEntry : mDriveLogList) { 
          writer.write(logEntry + "\n"); 
         } 
         writer.close(); 
        } catch (IOException e) { 
         Timber.e(e, "Error During Drive Contents Callback"); 
        } 

        MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
          .setTitle(SURVEIL_DROID + " " + DateHelper.getDate()) 
          .setMimeType("text/plain") 
          .setStarred(true).build(); 

        MetadataChangeSet folderSet = new MetadataChangeSet.Builder() 
          .setTitle("SurveilCustomFolder") 
          .build(); 

        Drive.DriveApi.getRootFolder(mGoogleApiClient) 
          .createFolder(mGoogleApiClient, folderSet) 
          .setResultCallback(folderCallback); 

         mDriveFolder = mFolderId.asDriveFolder(); 

         mDriveFolder.createFile(mGoogleApiClient, changeSet, mDriveContents) 
           .setResultCallback(fileCallback); 

        mDriveLogList.clear(); 
       } 
      } 
     }.start(); 

回答

1

你可以使用的CountDownLatch(見https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html),以幫助同步。像

protected void onPause() { 
    // TODO Find the right place for this 
    if (!mDriveLogList.isEmpty()) { 

     // Perform I/O off the UI thread. 
     // TODO Fix to use RxJava 
     new Thread() { 
      @Override 
      public void run() { 
       final protected CountDownLatch connectionLatch = new CountDownLatch(1); 
       protected boolean folderCreated = false;     
       Timber.i("Starting File Creation"); 
       // write content to DriveContents 
       if (mDriveContents == null) { 
        Timber.e("Drive Contents Were Null"); 

       } else { 
        OutputStream outputStream = mDriveContents.getOutputStream(); 
        Writer writer = new OutputStreamWriter(outputStream); 
        //TODO Clean/Refactor 
        try { 
         writer.write("Status Log For: " + DateHelper.getDate() + "\n"); 
         for (String logEntry : mDriveLogList) { 
          writer.write(logEntry + "\n"); 
         } 
         writer.close(); 
        } catch (IOException e) { 
         Timber.e(e, "Error During Drive Contents Callback"); 
        } 

        MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
          .setTitle(SURVEIL_DROID + " " + DateHelper.getDate()) 
          .setMimeType("text/plain") 
          .setStarred(true).build(); 

        MetadataChangeSet folderSet = new MetadataChangeSet.Builder() 
          .setTitle("SurveilCustomFolder") 
          .build(); 

        Drive.DriveApi.getRootFolder(mGoogleApiClient) 
          .createFolder(mGoogleApiClient, folderSet) 
          .setResultCallback(new 
           ResultCallback<DriveFolderResult>() { 
            @Override 
            public void onResult(DriveFolderResult result) { 
             folderCreated = result.getStatus().isSuccess(); 
             connectionLatch.countDown(); // Release latch         
            } 
           }); 
        try { 
         connectionLatch.await() ; 
        } catch (InterruptedException e) {    
         e.printStackTrace(); 
         return; 
        } 
        if (folderCreated) { 
         mDriveFolder = mFolderId.asDriveFolder(); 
         mDriveFolder.createFile(mGoogleApiClient, changeSet, mDriveContents) 
            .setResultCallback(fileCallback);       
         mDriveLogList.clear(); 
        } 
       } 
      } 
     }.start(); 
    } 
} 

應該工作。