I tried to use NSOperation and NSOperationQueue.
But As getting into background, NSOperation didn't work. it stopped.
Calling
- (void)viewDidLoad
{
findFQueue = [[NSOperationQueue alloc] init];
FindFeatureOperation *testOperation = [[FindFeatureOperation alloc] init];
[findFQueue addOperation:testOperation];
}
NSOperation define
#import "FindFeatureOperation.h"
@implementation FindFeatureOperation
- (void)main
{
for (int i = 0; i < 999999 ; i++) {
NSLog(@"Operation %d", i);
[NSThread sleepForTimeInterval:1];
}
}
@end
Let me Know Please.
to keep just running the operation:
- (main) {
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
for (int i = 0; i < 999999 ; i++) {
NSLog(@"Operation %d", i);
[NSThread sleepForTimeInterval:1];
}
[application endBackgroundTask];
}
- (void)applicationDidEnterBackground:(UIApplication *)app {
_bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// When Expired the limit time for background NSLog(@"Expired Background Task Limit Time");
[app endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[findFQueue waitUntilAllOperationsAreFinished];
[app endBackgroundTask];
});
}
Note: you can still be canceled if you use too much cpu / men / energy ... see the docs
Note 2: This can be written anywhere. I placed it in the OP but maybe in the VC is a better place... I don't think so but might be
Note 3: Read the docs! :: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html