【问题标题】:How to use background queue for Google Drive service in Objective C如何在 Objective C 中为 Google Drive 服务使用后台队列
【发布时间】:2016-04-04 02:32:41
【问题描述】:

根据documentationgoogle-api-objectivec-client库:

通过提供后台队列,可以在后台线程上回调来自任何线程的查询,如下例所示:

service.delegateQueue = [[NSOperationQueue alloc] init];

当指定了委托队列时,不需要运行循环在执行查询的线程上运行。

但是,它不起作用。处理程序仍然在主线程上执行。

问题:

如何告诉 Google Drive 服务在后台线程上执行处理程序?

代码 sn-p 重现

Pod 文件:

pod 'GTMOAuth2'
pod 'GoogleAPIClient/Drive'

应用中的某处:

#import "GTLDrive.h"
#import "GTMOAuth2Authentication.h"

...

- (void) applicationDidFinishLaunching:(NSNotification *) aNotification {
    service = [[GTLServiceDrive alloc] init];
    service.retryEnabled = YES;
    service.authorizer = _authorizer //from GTMOAuth2WindowController
    service.delegateQueue = [[NSOperationQueue alloc] init];

    GTLDriveFile * tempadFolder = [GTLDriveFile object];
    folder.name = @"folder-name";
    folder.mimeType = @"application/vnd.google-apps.folder";
    GTLQueryDrive * query = [GTLQueryDrive queryForFilesCreateWithObject: folder uploadParameters: nil];
    
    [service executeQuery: query completionHandler: 
                                       ^(GTLServiceTicket * ticket,
                                         GTLDriveFile * updatedFile,
                                         NSError * error) {
                                             if ([NSThread isMainThread]) {
                                                  NSLog(@"This is a main thread!");
                                             } 
                                         }
}

【问题讨论】:

    标签: objective-c multithreading background google-drive-api


    【解决方案1】:

    此错误已在 this commit 中修复并在 GoogleAPIClient 1.0.2 中发布。

    现在代码的行为根据文档:

    通过提供后台队列,可以在后台线程上回调来自任何线程的查询,如本例所示

    service.delegateQueue = [[NSOperationQueue alloc] init];
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2014-03-01
      • 2010-10-23
      • 2022-10-04
      • 1970-01-01
      • 2013-05-29
      • 2013-03-26
      • 2023-03-09
      • 2022-10-05
      相关资源
      最近更新 更多