目录

Performing Batch Operations

Use Cases

Drive Kit allows your app to perform batch operations using an API, including:

Creating a Drive Object

Call the Drive.Builder.build API to create a Drive object.
private Drive buildDrive() {   
    Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();   
    return service;   
}

Performing Operations in Batches

Create the JsonBatchCallback object and call the batch function of Drive to obtain the BatchRequest object. Then, call the queue function of the Files.Create method to add the list of operations you want to perform to the queue, and run BatchRequest.execute.
/**
 * Create a batch of callbacks.
 */  
JsonBatchCallback<File> callback = new JsonBatchCallback<File>() {
    @Override
    public void onFailure(JsonError e, HttpHeaders responseHeaders) throws IOException {
        Logger.i(TAG, "callback error: " + e.toString());
    }
 
    @Override
    public void onSuccess(File file, HttpHeaders responseHeaders) throws IOException {
        Logger.i(TAG, "callback successful: " + file.toString());
    }
};

/**
 * Create a batch of folders. 
 */  
private void batchCreateDirectory() {
    Drive drive = buildDrive();
    BatchRequest batch = drive.batch();
    try {
        for (int i = 0; i < 2; i++) {
            Map<String, String> appProperties = new HashMap<>();
            appProperties.put("appProperties", "property");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
            String dirName = formatter.format(new Date()) + "_" + i;
            Logger.i(TAG, "executeFilesCreate: " + dirName);
 
            File file = new File();
            file.setFileName(dirName)
                    .setAppSettings(appProperties)
                    .setMimeType("application/vnd.huawei-apps.folder");
            Drive.Files.Create create = drive.files().create(file);
            create.queue(batch, callback);
        }
        batch.execute();
    } catch (Exception e) {
        Logger.e(TAG, "batchCreateDirectory error: " + e.toString());
    }
}