如何在 Objective C 中为 Google Drive 服务使用后台队列

How to use background queue for Google Drive service in Objective C

根据documentation of google-api-objectivec-client图书馆:

Queries made from any thread can be called back on a background thread by providing a background queue, as in this example:

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

When a delegate queue is specified, there is no requirement for a run loop to be running on the thread that executes the query.

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

问题:

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

要重现的代码片段

播客文件:

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!");
                                             } 
                                         }
}

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

目前代码的行为符合文档:

Queries made from any thread can be called back on a background thread by providing a background queue, as in this example

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