P P
P P

Reputation: 41

android google drive api - query files return 0 file for the first time, second time it is ok

I have android app which create file and read file on google drive (synchronization sql lite db between devices). Create file is running ok. Read file is ok too but only for second attempt. First time my query returns always 0. It looks like first time query check only local storage?

Example: I create export from mobile. It is ok. I can see that file was created and i see it for example via web on google drive. I can see it also via android drive app. So I can do import from this file from my tablet from my app. First attempt is every time failed. Query could not find the file. Second attempt: File was find and imported. Why is this behaviour?

Query is creating like this:

Query query = new Query.Builder().addFilter(Filters.and(
Filters.eq(SearchableField.MIME_TYPE, "text/xml"),
Filters.eq(SearchableField.TITLE,
getResources().getString(R.string.app_file_name)),
Filters.eq(SearchableField.TRASHED, false))).build();

Drive.DriveApi.query(mGoogleApiClient, query)
.setResultCallback(metadataCallback);

and read file is like this in callback result:

MetadataBuffer mdbf = null;
mdbf =  result.getMetadataBuffer();
int iCount = mdbf.getCount();
tvout("file count: "+String.valueOf(iCount));
if (iCount == 1){
myFileId = mdbf.get(0).getDriveId();
Log.i(LOG_TAG, "file was found");
readFile();
}
else
{
displayAlertBox(getApplicationContext().getResources()
.getString(R.string.import_alert_res_not_ok)+" (ER-NOFILE)");
}

I do it like it is implemented in google drive api example - query file. I did really a lot of tests also with sync function. But every time my iCount is 0 for the first time. Second time it is 1. File was found.

Upvotes: 4

Views: 710

Answers (1)

Priyank Patel
Priyank Patel

Reputation: 12382

It's because of, The local sync takes a little bit to process. If you make a request before its done, you may get incomplete results. You can use requestSync to wait for a sync to have completed.

Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
     @Override
     public void onResult(@NonNull Status status) {       
         if (status.getStatus().isSuccess()) {
              // Do you work here
         }
     }
  });
}

Note that, Don't try sync operation too often.

In order to avoid excessive load on the device and the server, sync requests are rate limited. In this case, the operation will fail with DRIVE_RATE_LIMIT_EXCEEDED status, which indicates that a sync already happened quite recently so there is no need for another sync. The operation will succeed when reattempted after a sufficient backoff duration.

Upvotes: 1

Related Questions