Tuesday, April 12, 2016

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

Leave a Comment

According to documentation of google-api-objectivec-client library:

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.

But, it does not work. Handlers are still executed on a main thread.

Question:

How to tell Google Drive service to execute handlers on the background thread?

Code snippet to reproduce

Podfile:

pod 'GTMOAuth2' pod 'GoogleAPIClient/Drive' 

Somewhere in application:

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

1 Answers

Answers 1

This bug was fixed in this commit and released in GoogleAPIClient 1.0.2.

For now code behaves according to documentation:

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]; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment