此问题针对Google Drive Android API support team。它可能被认为是一个错误或只是单挑记录。在测试GDAA时,我遇到了这个问题:
关键是:文件搜索或元数据检索是否应该表明文件尚未就绪/不存在?或者有一种检查方法吗?
以下是用于演示问题的代码片段(简化 - 不是生产级代码)
原始#1创建一个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;
}
});
}
});
}
原始#2按名称查找文件 - 等待
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;
}
测试/问题情景:
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
答案 0 :(得分:0)
我很高兴地报告问题消失了(通过干预?)。正在创建(异步)的文件上的按名称查询在创建完成之前不会返回有效的元数据。上面原始#2的简化代码块:
for (Metadata md : rslt.getMetadataBuffer())
; // NO md AVAILABLE until the file creation is completed!!!
现在可以正常工作,在文件准备就绪之前不会枚举任何元数据。