当我的应用程序进入后台时,我可以将循环放入dispatch_async吗?

时间:2013-06-05 23:43:49

标签: ios objective-c uilocalnotification core-telephony

我想为iOS构建一个应用程序。我们的想法是在呼叫状态为UILocalNotification时启动Connected。我尝试在方法dispatch_async中使用applicationDidEnterBackground块,然后在此块中添加一个循环while(!isConnected),以了解何时应启动UILocalNotificacion。循环检查呼叫状态并决定何时启动通知。

我需要知道的是,这是解决问题的正确方法吗?还是有人有更好的主意?

好的,这是我的代码:

- (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.
isAlive = YES;

NSLog(@"BackgroundTimerRemaining %f", [application backgroundTimeRemaining]);

// verufy status of call
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you
    // stopped or ending the task outright.
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

[self readLocalVariableCalling];

__block int connected = 0;

__block int callingTemp = 1;//((NSNumber*)[self.datos objectForKey:@"calling"]).intValue;
NSLog(@"callingTemp: %d", callingTemp);

__strong typeof(self) weakSelf = self;

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool
    {
        //isAlive = YES;
        // Do the work associated with the task, preferably in chunks.
        while (callingTemp == 1)
        {
            if (callCenter != nil)
            {

            callCenter.callEventHandler = ^(CTCall* call)
            {
                NSLog(@"Loop - Call EventHandler state: %@", call.callState);
                if([call.callState isEqualToString:CTCallStateDisconnected])
                {

                    NSLog(@"disconnected");
                    [[UIApplication sharedApplication] cancelAllLocalNotifications];
                    callingTemp = 0;
                    connected = 0;
                    [weakSelf.datos setObject:[NSNumber numberWithInt:0] forKey:@"calling"];
                    [weakSelf saveLocalVariableCalling];

                }
                if ([call.callState isEqualToString:CTCallStateDialing] &&
                    connected == 0)
                {
                    NSLog(@"connected - Call EventHandler state : %@", call.callState);
                    connected = 1;
                    if (callingTemp == 1
                        && weakSelf.timerTableViewController.currentTimer)
                    {

                        TimeDto *currentTimer = weakSelf.timerTableViewController.currentTimer;
                        NSInteger seconds = currentTimer.value.intValue;

                        NSLog(@"minutos a segundos = %d", seconds);

                        NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:seconds];
                        UIApplication* app = [UIApplication sharedApplication];

                        if (weakSelf.notifyAlarm)
                        {
                            weakSelf.notifyAlarm.fireDate = alertTime;
                            weakSelf.notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
                            weakSelf.notifyAlarm.repeatInterval = 0;
                            weakSelf.notifyAlarm.soundName = @"road.caf";

                            NSString *messageTimer = NSLocalizedString(@"messageTimer", nil);
                            weakSelf.notifyAlarm.alertBody = [NSString stringWithFormat:messageTimer, ((int)currentTimer.value.intValue / 60 ) , (currentTimer.value.intValue % 60)];

                            [app scheduleLocalNotification:weakSelf.notifyAlarm];
                        }
                    }
                }


            };
            }
            else
            {
                NSLog(@"callCenter is null");
            }
            //NSLog(@"loop call state");

        }

        NSLog(@"Call Finished");


        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }

});
}

1 个答案:

答案 0 :(得分:3)

无论您如何安排工作,一旦您的应用程序处于后台,在所有活动停止之前,只允许进行一定量的进一步处理。试图dispatch_async并不会神奇地为你购买永久性的多任务处理。

所以,不,我不认为这可以解决你的问题。