作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为 iOS 构建一个应用程序。这个想法是在调用状态为 Connected
时启动 UILocalNotification
。我尝试在方法 applicationDidEnterBackground
中使用 dispatch_async
一个 block ,然后在这个 block 中放入一个循环 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;
}
});
}
最佳答案
无论您如何安排工作,一旦您的应用程序进入后台,在所有事件停止之前,它只允许进行一定数量的进一步处理。尝试 dispatch_async
不会神奇地为您带来永久的多任务处理。
所以,不,我不认为这可以解决您的问题。
关于ios - 当我的应用程序进入后台时,我可以在 dispatch_async 中加入一个循环吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951631/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!