在后台和 Nstimer 中工作时出现问题?
您好,我有一个任务是使用 Nstimer 和后台进程开发应用程序。
我已经用计时器实现了后台进程。它执行得很好。但是当我第一次最小化应用程序时,我遇到了问题,当时它没有运行后台进程。尽量减少涂抹 3 至 4 次后。之后就可以顺利工作了。我还显示后台任务和计时器的代码如下。
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
NSLog(@"Application enter in background");
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
我的 updateCounter 方法如下:
- (void)updateCounter:(NSTimer*)timer {
NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
NSLog(@"uniqueid:%@",id);
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"dLatitude : %@", latitude);
NSLog(@"dLongitude : %@",longitude);
}
是否有任何问题相关代码请帮助我解决它。
Hi i have atask to develop the application with Nstimer and background process.
I have already implement background process with timer. And it is excuting good.but i have problem when i minimize application first time at that time it is not running the background process. after minimizing application 3 to 4 times. After that it is working smoothly. i also display the code of background task and timer as follow.
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
NSLog(@"Application enter in background");
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
}
And My updateCounter method is as given follow:
- (void)updateCounter:(NSTimer*)timer {
NSString *id = [[UIDevice currentDevice] uniqueIdentifier];
NSLog(@"uniqueid:%@",id);
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"dLatitude : %@", latitude);
NSLog(@"dLongitude : %@",longitude);
}
Is their any problem related code Please help me to solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当应用程序处于后台状态时,
NSTimer
会暂停。您必须启动一些后台任务才能完成您想要的操作。但即便如此,应用程序置于后台后,您仍将受到一定时间的限制。
真正的后台行为仅适用于位置跟踪、VoIP 或音频应用程序。
其他应用程序必须面临限制:一旦进入后台,您就会有一定的时间来完成通过
beginBackgroundTaskWithExpirationHandler:
(backgroundTimeRemaining
) 启动的任务。整个事情在iOS应用程序编程指南中描述,在后台执行代码,特别是这里。
NSTimer
are paused when the app is in background state.You'll have to start some background task to do what you want. But even with that, you will be limited to a certain amount of time after the app was put in background.
Real backgrounding behavior is only granted for location tracking, VoIP or Audio apps.
Other apps must face limitations: once in background, you are given an amount of time to complete tasks you start with
beginBackgroundTaskWithExpirationHandler:
(backgroundTimeRemaining
).The whole thing is described in iOS Application Programming Guide, Executing Code in the Background, especially here.
首先:当您的应用程序进入后台时,计时器将无法按您的预期工作。
(取决于运行循环的可用性和超时)
从我从您的代码中收集到的信息来看,您似乎希望在应用程序在后台运行时进行位置更新。为此,您应该从此处查看指南:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24
First of all: Timers won't work as you expect when your app goes in background.
(depends on the availability of the run loop and your timeout)
From what i gather from your code, seems you like to have location update when the app is running in background. For this, you should check the guidelines from here:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24
检查您的应用程序何时进入后台模式以及何时进入前台,计算此差异并将经过的时间添加到计时器中,以便获得大约的总时间。我也实现了同样的方法,并且最终效果很好。
Check when your app goes to background mode and when it came into foregroud, calculate this difference and add the elapsed time in your timer so that you get an approx total time. I have also implemented the same and its working pretty well in my end.