Creating a continuous background stream in iOS

I have a requirement to create a background processor that only works when the application is in active mode. I tried to make the skeleton of what I am trying to achieve, but could not get it to work.

I want this background processor to stop sleeping when the application goes to the inactive stage and resume when the application goes into active mode. I provided the skeleton of what I did below. Can someone help me fix this.

Appdelegate.h

#import <Foundation/Foundation.h> @class BackgroundProcessor; @interface AppDelegate_iPhone : UIResponder<UIApplicationDelegate>{ BackgroundProcessor* processor; } @property(nonatomic) BackgroundProcessor *processor; @end 

AppDelegate.m

 #import "AppDelegate_iPhone.h" #import "BackgroundProcessor.h" @implementation AppDelegate_iPhone @synthesize processor; -(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { processor = [[BackgroundProcessor alloc]init]; [processor Start]; return YES; } -(void) applicationDidEnterBackground:(UIApplication *)application { [processor Sleep]; NSLog(@"Entered Background"); } -(void) applicationDidBecomeActive:(UIApplication *)application { [processor Resume]; NSLog(@"Became Active"); } @end 

BackgroundProcessor.h

 #import <Foundation/Foundation.h> @interface BackgroundProcessor : NSObject { NSThread* processor; } @property (nonatomic) NSThread *processor; -(void) Start; -(void) Sleep; -(void) workloop; -(void) Resume; @end 

BackgroundProcessor.m

 #import "BackgroundProcessor.h" @implementation BackgroundProcessor @synthesize processor; -(id) init { self = [super init]; if(self) { processor = [[NSThread alloc] initWithTarget:self selector:@selector(workloop) object:nil]; } return self; } -(void) Start { if(processor) [processor start]; } -(void) Sleep { // [processor [NSThread sleepForTimeInterval: 0.1]; } -(void) workloop { NSLog(@"Background Processor Processing ...."); [NSThread sleepForTimeInterval:0.1]; } - (void) Resume { NSLog(@"Background Resuming ...."); [NSThread sleepForTimeInterval: 0.1]; } 

I can't get a workloop to make it work all the time. Appreciate if someone can help me decide why the background

Tried it on the advice of Joshua Smith

 #import "BackgroundProcessor.h" @implementation BackgroundProcessor -(id) init { self = [super init]; if(self) { queue = [[NSOperationQueue alloc] init]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(workloop) object:nil]; [queue addOperation:operation]; } return self; } -(void) workloop { NSLog(@"Sleeping for 10 seconds"); sleep(10); NSLog(@"Background Processor Processing ...."); NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(workloop) object:nil]; [queue addOperation:operation]; } @end 
+4
source share
5 answers

If there is any specific reason, it should be a discrete stream, I would recommend using GCD or NSOperation Queue and producing and consuming workers. This kind of long thread will be a real problem in the iOS app.

GCD tutorial:

http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

+4
source

I applied a similar scenario to what you described. The way it was implemented was for the continuous timer to work in the background thread.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ notification = [[NotificationCentre alloc] init]; notificationTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(notificationTimerFired:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] run]; }); [self.window makeKeyAndVisible]; return YES; } -(void)notificationTimerFired:(NSTimer *)theTimer { BOOL hasNotifications; if (notificationTimerActive == YES) { do { hasNotifications = [notification GetNotifications]; } while (hasNotifications == YES); } return; } 

So basically when the application is running, I start the async re-timer and then call the GetNotifications method in my noticiationCenter class.

+2
source

By default, all processes will stop when you enter the background. There are just a few different processes that can be done in the background. Even though you are creating a new thread, it will still be terminated when you enter the background.

0
source

From the comment

I basically want to create a thread that continues to check the database periodically send data from my application to the server. What would you recommend the perfect option to do something like this. Is there an article or code that I can refer to having a workaround instead of creating my own threads? And this operation should only happen when the application is in Forefront mode or in active mode.

I would say that here you are using the wrong approach. Just start the asynchronous operation at regular intervals when the application is in the foreground to update the server. Using a long-lived background thread for this use case is incorrect.

0
source
 - (void)applicationDidEnterBackground:(UIApplication *)application { [[NSRunLoop currentRunLoop] run]; } 
0
source

All Articles