NEW Google Drive Android API - Metadata of unfinished file prematurely available

瘦欲@ 提交于 2020-01-15 04:15:12

问题


This problem is directed to the Google Drive Android API support team. It may be considered a bug or just heads-up note. When testing GDAA I've run into this problem:

  1. I created a file asynchronyously
  2. Before the file was ready, I performed a search and found the file by name
  3. Managed to retrieve it's metadata with no sign of problems
  4. Attempt to use the metadata blew up (obviously)

The point is: Shouldn't the file search or metadata retrieval indicate that file is not ready / does not exist yet? Or is there a method to check that?

Here are code snippets to demonstrate the problem (simplified - not a production level code)

primitive # 1 create a file asynch

  void createFileAsync(final DriveFolder fldr, final String name, 
                                                          final String mime, final byte[] buff) {
    final DriveId  dId = fldr.getDriveId();
    Drive.DriveApi.newContents(_gac).setResultCallback(new ResultCallback<ContentsResult>() {
      @Override public void onResult(ContentsResult rslt) {
        if (!rslt.getStatus().isSuccess()) return; 
        DriveFolder folder = Drive.DriveApi.getFolder(_gac, dId);
        MetadataChangeSet meta = new MetadataChangeSet.Builder()
                                                       .setTitle(name).setMimeType(mime).build();
        folder.createFile(_gac, meta, rslt.getContents()).setResultCallback(
                                                          new ResultCallback<DriveFileResult>() {
          @Override public void onResult(DriveFileResult rslt) {
            _dFile = rslt.getStatus().isSuccess() ? rslt.getDriveFile() : null; 
          }
        });
      }
    });
  }

primitive # 2 find the file by name - with wait

  public DriveFile findFirst(String name) {
    Filter filtr = Filters.and(
      Filters.eq(SearchableField.TRASHED, false),
      Filters.eq(SearchableField.TITLE, name)
    ); 
    Query qry = new Query.Builder().addFilter(filtr).build(); 
    MetadataBufferResult rslt = (Drive.DriveApi.query(_gac, qry).await()
    if (rslt.getStatus().isSuccess()) {
      MetadataBuffer mdb = null;
      try { 
        mdb = rslt.getMetadataBuffer();
        return Drive.DriveApi.getFile(_gac, mdb.get(0).getDriveId());          
      } finally { if (mdb != null) mdb.close(); } 
    }
    return null;
  }

test / problem scenario :

  GoogleApiClient _gac;   

  DriveFolder fldr = Drive.DriveApi.getRootFolder(_gac);
  byte[] buffer = ("FooBar ").getBytes();

  // create a file async
  DriveFile _dFile = null;
  createFileAsync(fldr, "foo", "text/plain", buffer);

  // file is not ready yet, but FOUND and it's metadata VALID (non-null)
  Metadata md = findFirst("foo").getMetadata(_gac).await().getMetadata();

  // any attempt to use Metadata methods 
  // md.isTrashed(), md.getTitle(), ...
  // blows up until the  createFileAsync() is finished

回答1:


I am happy to report that the problem disappeared (by intervention ?). The query by name on a file that is being created (asynchronously) does not return valid metadata until the creation is complete. Simplified code chunk from primitive #2 above:

for (Metadata md : rslt.getMetadataBuffer()) 
   ;  // NO md AVAILABLE until the file creation is completed!!! 

works correctly now, not enumerating any metadata until 'the file is ready'.



来源:https://stackoverflow.com/questions/22381649/new-google-drive-android-api-metadata-of-unfinished-file-prematurely-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!