-1

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.

nao0811ta
  • 183
  • 4
  • 11

1 Answers1

1

1 way: inside the op

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

2 way: using inside of applicationDidEnterBG:

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Thank you for responding. I have to use NSOperation. – nao0811ta Feb 16 '14 at 08:27
  • I soleved by using waitUntilAllOperationsAreFinished. Thank you. – nao0811ta Feb 16 '14 at 08:42
  • blocking on the main thread is not a valid way. IOS will kill you if you block 5s+ – Daij-Djan Feb 16 '14 at 09:21
  • @nao0811ta which is why use NSOperaton ^^ – Daij-Djan Feb 16 '14 at 09:22
  • Thank you. I didn't know 5s+ can block. So Do you know different way? Anyway, I don't mind blocking. background proccess is only a option. why i use nsoperation is my initial image proccessing takes much time. – nao0811ta Feb 16 '14 at 09:51
  • blocking is bad I said: if you block 5s you get killed by the OS and it won't work. use `beginBackgroundTaskWithExpirationHandler` like shown – Daij-Djan Feb 16 '14 at 10:15
  • I'll use beginBackgroundTaskWithExpirationHandler and NSOperation for continuing task in background. – nao0811ta Feb 16 '14 at 10:19
  • - (void)applicationDidEnterBackground:(UIApplication *)application {_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]; }); } – nao0811ta Feb 16 '14 at 10:20
  • your missing another endBackroundTask :: looking ok besides that :: - (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]; }); } – Daij-Djan Feb 16 '14 at 10:24
  • OP works on foreground and background. But Should I use beginBackgroundTaskWithExpirationHandler in OP? – nao0811ta Feb 16 '14 at 10:45
  • I decided to use second way. – nao0811ta Feb 16 '14 at 10:46