Sto cercando di verificare il valore di backgroundTimeRemaining
, ma ottengo un numero molto grande. Il valore della proprietà dovrebbe corrispondere a 10 min aprox e ho letto nella documentazione Apple here che tale valore è elevato quando l'app è in primo piano. Tuttavia, ottengo un valore elevato anche in background:iOS - backgroundTimeRemaining valore non mostra cosa previsto
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Background task
NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;
NSLog(@"Background time remaining: %f seconds (%d mins)", timeRemaining, (int)(timeRemaining/60));
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;
NSLog(@"Background time remaining: %f seconds (%d mins)", timeRemaining, (int)(timeRemaining/60));
// Perform task
// Finished
if (bgTask != UIBackgroundTaskInvalid) {
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
});
// Start location manager
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}
}
Cosa potrei mancare? Grazie in anticipo
Penso che l'attività in background non verrà avviata fino a quando l'evento 'applicationDidEnterBackground' è stato gestito completamente. – Felix
Quindi ... come posso richiedere il valore 'backgroundTimeRemaining'? – AppsDev
prova all'interno di un blocco dispatch_async sul thread principale – Felix