2013-06-10 2 views
1

Я пытаюсь запустить некоторые фоновые задачи после выхода из приложения.Фоновые задачи не работают

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4 
     if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking 
      UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance 
      __block UIBackgroundTaskIdentifier background_task; //Create a task object 
      background_task = [application beginBackgroundTaskWithExpirationHandler:^{ 
       [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks 
       background_task = UIBackgroundTaskInvalid; //Set the task to be invalid 
       //System will be shutting down the app at any point in time now 
      }]; 
      //Background tasks require you to use asyncrous tasks 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       //Perform your tasks that your application requires 
       NSLOG(@"test1"); 
       [self performSelector:@selector(checkActivityCount) withObject:nil afterDelay:3]; 
       [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform 
       background_task = UIBackgroundTaskInvalid; //Invalidate the background_task 
      }); 
     } 
    } 
} 


-(void)checkActivityCount{ 
    NSLog(@"test"); 
    NSString *urlstring = @"https://exampleapp.com/api/v1/postactivity/?unreadfeedcount=yes"; 
    NSURL *url = [NSURL URLWithString:urlstring]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     BOOL success = [[JSON objectForKey:@"success"] boolValue]; 
     if (success) { 
      if (![JSON[@"unread_activity_count"] isEqualToString:@"0"]) { 
       // Schedule the notification 
       UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
       localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; 
       localNotification.alertBody = [NSString stringWithFormat:@"You have %@ unread activities.",JSON[@"unread_activity_count"]]; 
       localNotification.alertAction = @"Show me the item"; 
       localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
       localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

       [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

       // Request to reload table view data 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 
      } 
     } 
    } failure:nil]; 
    [operationQueue addOperation:operation]; 

    [self performSelector:@selector(checkActivityCount) withObject:nil afterDelay:3000]; 

} 

В настоящее время test1 регистрируется, но тест никогда не регистрируется. Я что-то делаю в коде?

ответ

1

Использование performSelector:withObject:afterDelay: подталкивает запрос в очередь для потока. Он не ждет этого. Поэтому сразу после этого (даже если что-то выберет элемент из очереди за 3 секунды), вы сообщаете приложению, что фоновая задача завершена, и приложение закрывается.

Я не пробовал паузу, как это, но попробовать что-то больше, как:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    [self checkActivityCount]; 
    [application endBackgroundTask: background_task]; 
    background_task = UIBackgroundTaskInvalid; 
}); 
+0

права. Вы завершаете фоновое задание перед выполнением 'performSelector:'. –

Смежные вопросы