Search code examples
iosobjective-cgoogle-apigoogle-drive-apibackground-process

Continue upload with GTLServiceTicket in the background


In the singleton responsible for network, I create an instance of drive service:

self.service = [[GTLServiceDrive alloc] init];
[self.service setShouldFetchInBackground:YES];

At some point I create a ticket to upload the given file:

GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithFileHandle:fileHandle MIMEType:kGDriveMimeType];
GTLDriveFile *newFile = [GTLDriveFile object];
newFile.title = @"123";

// keep the file inside event folder
GTLDriveParentReference *parentFolder = [GTLDriveParentReference object];
parentFolder.identifier = parentFolderId;
newFile.parents = @[parentFolder];

GTLQueryDrive *uploadQuery = [GTLQueryDrive queryForFilesInsertWithObject:newFile uploadParameters:uploadParameters];

GTLServiceTicket *serviceTicket = [self.service executeQuery:uploadQuery completionHandler:^(GTLServiceTicket *ticket, GTLDriveFile *uploadedFile, NSError *uploadError) {
    if (completionHandler) {
        completionHandler(uploadedFile, uploadError);
    }
}];

[serviceTicket setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long numberOfBytesRead, unsigned long long dataLength) {
    CGFloat progress = (CGFloat)numberOfBytesRead / (CGFloat)dataLength;
    if (progressHandler) {
        progressHandler(progress);
    }
}];

The whole problem is that when I'm downloading data using GTMHTTPFetcher the work in the background is continued. With uploading with GTLServiceTicket it is paused while the app is in the background. I cannot find any property to make it woking in the background. Am I missing something? How to do it?


Solution

  • I didn't find any suitable solution with GTLServiceTicket so I've finished up with custom wrapper which is listening to UIApplicationDidEnterBackgroundNotification and using beginBackgroundTaskWithName:expirationHandler:.