Reputation: 606
So I'm trying to fetch files and folders using Google Drive API.
- (void)fetchFilesFrom:(NSString *)folderID remoteFolderPath:(NSString *)folderPath {
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = [NSString stringWithFormat:@"'%@' IN parents", folderID]; // get all files and folders under a parent folder
query.fields = @"nextPageToken, files(id, name, mimeType, modifiedTime)"; // get files' and folders' name and id only
[self.serviceDrive executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *fileList,
NSError *error) {
if (error == nil) {
for (GTLDriveFile *file in fileList.files) {
if ([file.mimeType containsString:@"vnd.google-apps.folder"]) { // folder not file
NSString *subFolderPath = [NSString stringWithFormat:@"%@/%@", folderPath, file.name];
[self.driveDict setValue:file forKey:subFolderPath];
// Create the folder locally if not existing
[DocumentHandler checkToCreateDir:subFolderPath];
// Fetch files in a subfolder
[self fetchFilesFrom:file.identifier remoteFolderPath:subFolderPath];
}else if(![file.mimeType containsString:@"vnd.google-apps."]) {
// Ignore other google files like speadsheet... as they are undownloadable
NSString *filePath = [NSString stringWithFormat:@"%@/%@", folderPath, file.name];
[self.driveDict setValue:file forKey:filePath];
}
}
/* If possible, I want to have the full driveDict here in order to compare. */
} else NSLog(@"An error occurred: %@", error);
}];
}
The above function is working fine, but I don't want to use recursive loop to get all files in subfolders. Is there any way to get all files in the original and its subfolders with only 1 execution?
The reason I bring that up is I want to compare the list of remote files with local file list to delete unnecessary local files before downloading drive files. And using executeQuery:completionHandler:^{}
blocks gives me no way to successfully implement serial executions. The methods inside blocks are always executed later.
Upvotes: 0
Views: 864
Reputation:
Your Question same me. You can using block :)
@property (nonatomic, copy) void(^blockHandler)(id data);
// get list file
- (void)listFiles:(NSString *)fileId complete:(void(^)(id data))completion {
self.blockHandler = completion;
GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];
query.fields = @"nextPageToken, files(id, name, thumbnailLink, webViewLink)";
query.pageSize = 1000;
query.q = [NSString stringWithFormat:@"'%@' In parents and trashed=false",fileId];
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
- (void)displayResultWithTicket:(GTLRServiceTicket *)ticket
finishedWithObject:(GTLRDrive_FileList *)result
error:(NSError *)error {
if (error == nil) {
NSMutableString *output = [[NSMutableString alloc] init];
if (result.files.count > 0) {
[output appendString:@"Files:\n"];
for (GTLRDrive_File *file in result.files) {
itemGG *temp = [[itemGG alloc] initWithName:file.name linkThumb:file.thumbnailLink fileID:file.identifier];
[self.lstItem addObject:temp];
[output appendFormat:@"%@ (%@)\n", file.name, file.identifier];
}
} else {
[output appendString:@"No files found."];
}
NSLog(@"%@", output);
} else {
NSLog(@"Error getting presentation data: %@\n", error.localizedDescription);
}
if (self.blockHandler) {
self.blockHandler([[NSArray alloc] initWithArray:self.lstItem]);
}
}
__weak typeof(self) w = self;
[self.cloud listFiles:@"root" complete:^(id data) {
w.tableData = data;
[w.myCollection reloadData];
}];
Upvotes: 0
Reputation: 65
For more information and detailed steps Below is the Link for Google drive's Official site https://developers.google.com/drive/ios/quickstart
- (void)fetchFiles {
self.output.text = @"Getting files...";
GTLQueryDrive *query =
[GTLQueryDrive queryForFilesList];
//query.pageSize = 10; /* Total number of files to get at once But useful only when there are more than hundreds of file to get once.*/
query.fields = @"nextPageToken, files(id, name)";
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
Upvotes: 1
Reputation: 65
- (void)fetchFiles {
self.output.text = @"Getting files...";
GTLQueryDrive *query =
[GTLQueryDrive queryForFilesList];
//query.pageSize = 10; /* Total number of files to get at once But useful only when there are more than hundreds of file to get once.*/
query.fields = @"nextPageToken, files(id, name)";
[self.service executeQuery:query
delegate:self
didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
}
Call this method to fetch all the files and folders are in drives. It lists you all the folders, files including its sub folders and files in one call. In one word everything in root folder.
Upvotes: 0