代码之家  ›  专栏  ›  技术社区  ›  phạm tuấn

Objective-C在使用Google驱动程序API获取列表文件时使用block

  •  0
  • phạm tuấn  · 技术社区  · 9 年前

    我正在使用Google Drive API。当我使用演示获取文件列表时,我认为这个应用程序运行多线程。现在我想在方法成功时获取文件列表。

    - (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:)];
    }
    

    https://developers.google.com/drive/ios/quickstart

    1 回复  |  直到 9 年前
        1
  •  1
  •   user8410974 user8410974    9 年前

    你可以试试。

    @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];
    }];