简体   繁体   中英

Can I put a loop into dispatch_async when my app enters in background?

I want to build an application for iOS. The idea is to launch a UILocalNotification when a call state is Connected . I try to use dispatch_async a block in the method applicationDidEnterBackground and I put into this block a loop while(!isConnected) to know when should the UILocalNotificacion be launched. The loop checks the call state and decides when to launch the notification.

What I need to know is , is that way the correct way to solve my problem? or does anyone have a better idea?

Ok, this my code:

- (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;
    }

});
}

Regardless of how you schedule the work, once your application is in the background it's allowed only a certain amount of further processing before all activity is stopped. Trying to dispatch_async isn't magically going to buy you permanent multitasking.

So, no, I don't think this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM