Search code examples
iosgoogle-drive-apigoogle-api-objc-client

listing all files from specified folders in Google Drive through IOS Google Drive SDK


Actually i integrated google drive sdk with my ios app. I can able to retrieve/upload the files in google drive through google drive ios sdk. But the retrieving files list from specified parent folder taking so much time.

Here the steps and the code which was i am using.

First am getting the children's of the specified parent folder then am getting the GTLDriveChildReference of each child then then am querying with the child reference identifier.

This is huge process for me. Also it request google server each time. Any better way there to just pass the parent folder id in a query and pulling the files from that parent folder.

   -(void)getFileListFromSpecifiedParentFolder {
        GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:<some_id or root>];
        query2.maxResults = 1000;

        // queryTicket can be used to track the status of the request.
        [self.driveService executeQuery:query2
            completionHandler:^(GTLServiceTicket *ticket,
                            GTLDriveChildList *children, NSError *error) {
                NSLog(@"\nGoogle Drive: file count in the folder: %d", children.items.count);
                //incase there is no files under this folder then we can avoid the fetching process
                if (!children.items.count) {
                    return ;
                }

                if (error == nil) {
                    for (GTLDriveChildReference *child in children) {

                        GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];

                        // queryTicket can be used to track the status of the request.
                        [self.driveService executeQuery:query
                            completionHandler:^(GTLServiceTicket *ticket,
                                                GTLDriveFile *file,
                                                NSError *error) {

                                                NSLog(@"\nfile name = %@", file.originalFilename);
                                                }];
                    }
                }
            }];
    }

Any help that might be appreciated.


Solution

  • Try combining queries in a batch query, like

    GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
    for (GTLDriveChildReference *child in children) {
      GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
      query.completionBlock = ^(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error) {
        NSLog(@"error=%@, file name = %@", error, file.title);
      };
      [batchQuery addQuery:query];
    }
    [driveService executeQuery:batchQuery completionHandler:...]
    

    If automatic fetching of result pages is enabled (service.shouldFetchNextPages = YES) then try to set the maxResults property of queries to avoid the need for the library to make multiple fetches. For example, if there are 100 or fewer results for a query, a maxResults value of 100 will retrieve them with a single fetch.

    Requests for partial responses are also a bit faster and use much less memory, particularly for large results.

    Look at http logs for a better idea of the response sizes for your queries.