Using multiple calls to beginBackgroundTaskWithExpirationHandler

I try to follow this previous post here: Best practice sending large amounts of data in the background on an iOS4 device?

And basically, I have a method called getRequest that grabs information from a web server. The web server requires about 50 data units. Therefore, at the same time, I have 50 delegate calls to connectionDidFinishLoading. Currently my getRequest looks like this:

-(void) getRequestWithURL:(NSString *) requestURL 
{
    static int getRequest = 0;
    NSLog(@"getRequest: %i", getRequest);
    getRequest++;

    UIApplication *app = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier taskID;
    taskID = [app beginBackgroundTaskWithExpirationHandler:^{

        NSLog(@"Time remaining: %f", app.backgroundTimeRemaining);

        NSLog(@"Background task not completed");
        [app endBackgroundTask:taskID];
    }];

    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:requestURL]];
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self] ; 
    [self startRequestWithConnection:con];
    [req release];

    if (taskID == UIBackgroundTaskInvalid) {
        NSLog(@"Failed to create background task identifier");
    }

}

Then in my connection DidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // process data from server
   // endBackgroundTask:someTaskID???
}

, beginBackgroundTaskWithExpirationHandler, , , getRequest, __block UIBackgroundTaskIdentifier taskID . , endBackgroundTask connectionDidFinishLoading getRequest, startBackgroundTaskWithExpirationHandler endBackgroundTask:. , , getRequest ? 50 ivars connectionDidFinishLoading, 50 getRequest? .

+2
2

, beginBackgroundTaskWithExpirationHandler endBackgroundTask.

, , :

UIBackgroundTaskIdentifier backgroundTaskID;

, getRequest connectionDidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // process data from server
    getRequest--;

    if (getRequest == 0 && backgroundTaskID != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
        backgroundTaskID = UIBackgroundTaskInvalid;
    }
}

. , , .

UIApplicationDidEnterBackgroundNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidEnterBackground)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
    if (getRequest > 0) {
        backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
            [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
            backgroundTaskID = UIBackgroundTaskInvalid;
         }];
    }
}

, , , , , .

- NSOperationQueue, .

+5

, . . - , iOS, .

+1

Source: https://habr.com/ru/post/1625542/


All Articles