how to call function after every 1 hour?(Swift). B

2019-02-13 22:16发布

问题:

i am making app. which providing functionality of fetch data after every one hour. so even when my app terminate how can i call that function?

after too much searching i found Background fetching by performFetchWithCompletionHandler but i want to know that this function will work if my app is terminated?what i mean is if my app is in closed or terminated state how can i call function automatically?

回答1:

You can't do anything if your app is in terminated state. Apple is not allowing anything (except receiving notification) in terminated or closed state.

You are also restricted for background execution also. You can perform specific kind of task in background mode like : Location update,Music playing, VOIP, finite length task, Newsstand downloads, etc. You can refer apple documentation for more details about background execution.

Hope this will help :)



回答2:

Once I needed this same when I was working on some project, unfortunately I didn't find any solution for it.

You can run a task when your app is in Background state(that too with limited services) but apple doesn't allow to run a task when app is Terminated

Please go through the link.

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

and this is for background running. Sending Latitude and Longitude to Server when app is in background



回答3:

Your app is terminated once it will not do any kind of task related to it. Apple Doesn't support it. You can not call Functions when your app is not running. So it's not possible Due to apple rules. performFetchWithCompletionHandler is only work when your app is running if your application is terminated it will not perform any kind of task.



回答4:

From what i have experienced:

First of all the timer will stop when the app goes to the background to stop fast battery drainage and expand the lifetime of the battery even to save network bundles quota consumption while you are not actively using the app.

You can do background fetch tasks by adding the following in the AppDelagate "as you mentioned":

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    //Do whatever you need
    //This will keep running for max 3 mins
}

But the same is for background fetch tasks. You have got maximum 3 min of background activity except for Locations services, Audio playing, VOIP and some other tasks.

I really don't know the use and the functionality of the app and why do you need to grab data from the server every 1hr if the app is already closed and the user is not concerned about seeing the updated data.

But if this data is important to be updated every hour and need to notify the user with the updates "for instance checking the status of a certain event on the server, like making a chat app and the user received a message". Then it is better to send a remote push notification to the user from the server upon a certain event taking place and this will make the user get the app again on the foreground and the app will start fetching the updated data "in my case here the message".



回答5:

you can find location in app terminated state, after significantly change in location like about 500 meter. here is working example

http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

but what I am stuck at is post location through webservice.



回答6:

@Jack.Right you can call the method after every 1 hour so use NSTimeinterval it would be helpful!!

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];

    //create new uiBackgroundTask
    __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    //and create new timer with async call: 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //run function methodRunAfterBackground
        NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(methodRunAfterBackground) userInfo:nil repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });
}