问题
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:
- I created a file asynchronyously
- Before the file was ready, I performed a search and found the file by name
- Managed to retrieve it's metadata with no sign of problems
- 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