2013-06-05 29 views
0

我想爲iOS構建一個應用程序。這個想法是當呼叫狀態是Connected時啓動UILocalNotification。我嘗試在方法applicationDidEnterBackground中使用dispatch_async塊,並且我在此塊中放入了一個循環while(!isConnected)以知道何時應該啓動UILocalNotificacion。循環檢查呼叫狀態並決定何時啓動通知。當我的應用程序進入後臺時,可以將一個循環放入dispatch_async嗎?

我需要知道的是,這是解決我的問題的正確方法嗎?還是有人有更好的主意?

好吧,這是我的代碼:

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

}); 
} 
+0

我真的不喜歡忙等待的想法。檢查http://stackoverflow.com/questions/3319805/detecting-call-state-in-ios4 –

+0

好的,但這是在iOS 4,現在我們有iOS 6,我想我可能是一個可能的解決方案 – kakashy

+0

其實,我的值得關注的是'while(!isConnected)'會浪費處理器的週期。 –

回答

3

不管你如何安排工作,一旦你的應用程序在後臺它只是所有活動停止之前,允許一定量的進一步處理。嘗試dispatch_async並不奇妙地會爲您購買永久性多任務。

所以,不,我認爲這不能解決您的問題。

+0

好吧,但我知道'dispatch_async'有一個超時(10分鐘)我認爲時間已經足夠我的目的,你不覺得嗎? – kakashy

+1

它沒有正式記錄超時。你可以使用'beginBackgroundTaskWithExpirationHandler'來請求一些後臺處理,但你會花費的時間我認爲這個問題的根源在於你想要做的事情明確地是蘋果不希望你做的事情,這是爲了應用程序商店嗎? – Tommy

+0

沒有記錄的時間是2分鐘,我相信 – Sulthan

相關問題