2014-07-24 45 views
1

我使用Google Drive API查询文件并检索其元数据。当在之前从未安装的设备上运行应用程序时,返回的MetadataBuffer为空(正如我从logcat中看到的那样)。 如果我卸载应用程序并将其重新安装回,则相同的代码将生成一个带有预期的元数据对象的MetaDataBuffer。这发生在我测试的每个设备上。我认为它与本地缓存有关。我想知道我是否在我的代码中丢失了某些东西,或者我最终应该向Google提交问题。
下面是完整的代码:
Google云端硬盘查询不检索文件 - 仅第一次

public void setupClient(boolean noProgress) { 
    Log.i("drive", "setup client..."); 
    mGoogleApiClient = new GoogleApiClient.Builder(parent) 
      .addApi(Drive.API) 
      .addScope(Drive.SCOPE_FILE) 
      .addConnectionCallbacks(connectionCallbacks) 
      .addOnConnectionFailedListener(connectionFailedListener) 
      .build();   
    mGoogleApiClient.connect(); 
} 

private GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() { 
    @Override 
    public void onConnected(Bundle bundle) { 
     Log.i("drive", "connected");   
     Filter mime = Filters.eq(SearchableField.MIME_TYPE, MIME_TYPE); 
     Filter trashed = Filters.eq(SearchableField.TRASHED, false); 
     Query query = new Query.Builder() 
       .addFilter(mime) 
       .addFilter(trashed) 
       .build(); 
     Drive.DriveApi.query(mGoogleApiClient,query).setResultCallback(metadataCallback); 
    } 

private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback = new 
     ResultCallback<DriveApi.MetadataBufferResult>() { 
      @Override 
      public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {      
       MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer();           
       Log.i("drive", String.valueOf(buffer.getCount()));     
       if (buffer.getCount()>0) { 
       /* this is where it fails: 
        the first time, getCount() is 0. the following times, 
        getCount() is the right number of files in the Drive */ 
        Log.i("drive","files found"); 
        //do things with metadata... 
       } else {       
        Log.i("drive","files not found"); 
        //do other things when there are no files... 
       } 

      } 
     }; 
+0

我有同样的问题,但我可以每次安装应用程序时重现它 – Simon

回答

0

本地同步需要一点点来处理。如果您在完成之前提出请求,您可能会得到不完整的结果。您可以使用requestSync等待同步完成。 (虽然有时候这个请求的速度有限,如果已经有一个请求正在进行,我们正在努力改善这种行为。)

+0

requestSync似乎解决问题。非常感谢你的提示 – devrocca

+0

你可以写例子如何“requestSync”? – Michal

+0

@Michal只需调用'Drive.DriveApi.requestSync(googleApiClient)'。但请注意,请求限制非常有限。如果我在两次通话之间等待不到一分钟,我会收到错误“DriveStatusCodes.DRIVE_RATE_LIMIT_EXCEEDED”。延迟没有记录,并且使设备之间同步应用数据不切实际。 – sorianiv

相关问题