【问题标题】:Can I put a loop into dispatch_async when my app enters in background?当我的应用程序进入后台时,我可以在 dispatch_async 中添加一个循环吗?
【发布时间】:2017-11-27 14:21:28
【问题描述】:

我想为 iOS 构建一个应用程序。这个想法是在呼叫状态为Connected 时启动UILocalNotification。我尝试在方法applicationDidEnterBackground 中使用dispatch_async 一个块,并在该块中放入一个循环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;
    }

});
}

【问题讨论】:

  • 我真的不喜欢忙着等待的想法。检查stackoverflow.com/questions/3319805/…
  • 好的,但这是在 iOS 4 中,现在我们有 iOS 6,我想我可能是一个可能的解决方案
  • 其实我担心while(!isConnected)会浪费你的处理器周期。
  • 也许你是对的,但我认为 while(!isConnected) 没有运行太多时间,因为当呼叫状态为 Connected 时循环中断。
  • 为什么我的问题出现负面反弹?,我应该有权问,因为没有问题是坏的:(

标签: ios objective-c uilocalnotification core-telephony


【解决方案1】:

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

所以,不,我认为这不能解决您的问题。

【讨论】:

  • 好的,但是我知道dispatch_async 有一个超时(10 分钟)我认为这个时间对于我的目的来说已经足够了,你不觉得吗?
  • 它没有官方记录的超时。您可以使用beginBackgroundTaskWithExpirationHandler 请求一些后台处理,但没有记录您将被允许的时间量。我认为,问题的根源在于你想做的事情显然是苹果不希望你做的事情。这是应用商店的吗?
  • 我相信无证时间是2分钟
猜你喜欢
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
相关资源
最近更新 更多