Call the server every minute while app in Foreground

Tamil_Arya

i want to call my server every minute while app in foreground.this call should not be depends on any ViewController class as be common method.

(ex : call my server depends on server response i want to show some alert as commonly )

i just keep trying by app delegate method but i couldn't get any solution. is there any other ways or any method belongs to app delegate ??

Sandeep Bhandari

Tamil_Arya,

You should not dump all your code in AppDelegate just because it is singleton and availabe through out the application life cycle. Putting everything in appdelegate will make your code very much clumsy and follows a very bad design pattern.

Following MVC is one nice thing you can do to keep your code reliable and robust.

Anyway Here is what you can do,

I believe you must be having a singleton class for making webservice calls. If not create one.

For Example lets call the class as WebService.h and WebService.m

So your WebService.h should look like

@interface WebService : NSObject
+ (instancetype)shared; //singleton provider method
- (void)startSendPresence; //method you will call to hit your server at regular interval
- (void)stopSendPresence //method you will call to stop hitting
@end

WebService.m should look like

@interface WebService ()
@property (strong, nonatomic) NSTimer *presenceTimer;
@end

@implementation WebService

+ (instancetype)shared
{
    static id instance_ = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance_ = [[self alloc] init];
    });

    return instance_;
}

- (void)startSendPresence {

    [self sendPresence:nil]; //to make the first webservice call without waiting for timer to trigger

    if(!self.presenceTimer){
        self.presenceTimer = [NSTimer scheduledTimerWithTimeInterval:self.presenceTimerInterval target:self selector:@selector(sendPresence:) userInfo:nil repeats:YES];
    }
}

- (void)sendPresence:(NSTimer *)timer {
    //make your web service call here to hit server
}

- (void)stopSendPresence {
    [self.presenceTimer invalidate];
    self.presenceTimer = nil;
}
@end

Now your Webservice singleton class to hit webserver at regular interval is ready :) Now call it wherever you want it when you want to start hitting and call stopSendPresence when you want to stop it :)

Assuming you want to start hitting server as soon as Application comes to foreground always (Though does not make much sense to me hope it makes to you)

In your AppDelegate.m

//this method will be called when you launch app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[WebService shared] startSendPresence];
}

//this method will be called when you brimg it foreground from background
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[WebService shared] startSendPresence];
}

If you want to stop hitting sever as soon as your app goes to background

- (void)applicationDidEnterBackground:(UIApplication *)application {
     [[WebService shared] stopSendPresence];
}

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I bring Electron app to foreground every "N" minute?

How to call api url method so that app reloads every time the app enters from background to foreground?

i want to post location updates every 15mins to server even when the app is not running in the foreground

How to call an API every minute for a Dashboard in REACT

How to call a function every minute in a React component?

React hooks - manage API call every minute

ViewDidAppear not executed every time app comes to foreground

APNs: Change the app badge while the app is the foreground

RedHat server updates its time every minute

Cordova app that uploads my location every minute to a back-end server, even when screen is off or app is in background

Can Android stop a service while app is in foreground?

Get push notification while App in foreground iOS

Handling push notifications while watchOS app is in the foreground?

avoid showing alert for notification while app is in foreground

Why scenePhase value is .background while the app is in the foreground?

Call specific code on entering app foreground

Android: Get foreground app info from service every time a particular app enters foreground

When try call method every minute it called just twice

Make http request call every x minute in angular 9

how call a function every 5 minute inside array in javascript

How to call perl file every minute from batch file

I want a proxy in Apigee to call itself every 1 minute

Execute function in mobile app every few minutes, regardless of foreground/background

How to show alert in every ViewController when app is active (foreground)?

How to count app usage time while app is on foreground?

Show notification in iOS app While app in foreground / Active in swift 3

Continually running UIButton every minute even app is minimized

Phonegap display notification on specific time every minute even the app is not running

How to run a cron jobs in every minute for a specific hour on server