因此,我尝试使用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);
}];
}
上面的函数工作正常,但我不想使用递归循环来获取子文件夹中的所有文件。有没有办法让原始文件及其子文件夹中的所有文件只执行1次?
我提出这个问题的原因是我想在下载驱动文件之前将远程文件列表与本地文件列表进行比较以删除不必要的本地文件。使用executeQuery:completionHandler:^{}
块使我无法成功实现串行执行。块内部的方法总是稍后执行。
答案 0 :(得分:1)
更多信息和详细步骤以下是Google云端硬盘官方网站的链接 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:)];
}
答案 1 :(得分:0)
- (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:)];
}
调用此方法以获取驱动器中的所有文件和文件夹。它会在一次调用中列出所有文件夹,文件,包括子文件夹和文件。总之,根文件夹中的所有内容。
答案 2 :(得分:0)
你的问题和我一样。你可以用块:)
@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];
}];