如何每1小时调用一次函数?(Swift)应用程序终止时的后台取回工作?

时间:2021-12-20 01:09:33

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?

我正在制作app.它每一小时提供取数据功能。即使我的应用程序终止了我怎么调用这个函数呢?

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?

在搜索了太多之后,我找到了performFetchWithCompletionHandler的后台取回,但是我想知道如果我的应用程序终止了,这个函数会工作吗?我的意思是如果我的应用程序处于关闭或终止状态我怎么能自动调用函数?

6 个解决方案

#1


11  

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.

你也被限制在后台执行。您可以在后台模式下执行特定类型的任务,如:位置更新、音乐播放、VOIP、有限长度任务、报摊下载等。有关后台执行的详细信息,请参阅苹果文档。

Hope this will help :)

希望这能有所帮助:

#2


1  

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

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


1  

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.

一旦你的应用程序不再执行任何与它相关的任务,它就会被终止。苹果不支持它。当你的应用程序没有运行时,你不能调用函数。所以这是不可能的,因为苹果的规定。performFetchWithCompletionHandler只在你的应用程序运行时有效如果你的应用程序终止它将不会执行任何类型的任务。

#4


0  

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.

首先,当app进入后台时,定时器会停止,停止快速的电池排水,延长电池寿命,甚至在你不积极使用app的情况下,也可以节省网络捆绑的配额消耗。

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

您可以通过在AppDelagate中添加以下内容来完成后台取回任务:

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.

但后台取回任务也是如此。除了位置服务、音频播放、VOIP和其他一些任务之外,您的后台活动最多只有3分钟。

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.

我真的不知道app的用途和功能,如果app已经关闭,用户不关心看到更新的数据,为什么每1小时就要从服务器上抓取数据。

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


0  

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

你可以在app端端状态中找到位置,在位置发生显著变化后,比如500米。这是工作示例

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

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.

但我被困在的是通过webservice发布位置。

#6


-3  

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

@Jack。是的,你可以在每1小时之后调用这个方法,所以使用NSTimeinterval会很有帮助!

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

#1


11  

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.

你也被限制在后台执行。您可以在后台模式下执行特定类型的任务,如:位置更新、音乐播放、VOIP、有限长度任务、报摊下载等。有关后台执行的详细信息,请参阅苹果文档。

Hope this will help :)

希望这能有所帮助:

#2


1  

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

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


1  

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.

一旦你的应用程序不再执行任何与它相关的任务,它就会被终止。苹果不支持它。当你的应用程序没有运行时,你不能调用函数。所以这是不可能的,因为苹果的规定。performFetchWithCompletionHandler只在你的应用程序运行时有效如果你的应用程序终止它将不会执行任何类型的任务。

#4


0  

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.

首先,当app进入后台时,定时器会停止,停止快速的电池排水,延长电池寿命,甚至在你不积极使用app的情况下,也可以节省网络捆绑的配额消耗。

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

您可以通过在AppDelagate中添加以下内容来完成后台取回任务:

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.

但后台取回任务也是如此。除了位置服务、音频播放、VOIP和其他一些任务之外,您的后台活动最多只有3分钟。

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.

我真的不知道app的用途和功能,如果app已经关闭,用户不关心看到更新的数据,为什么每1小时就要从服务器上抓取数据。

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


0  

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

你可以在app端端状态中找到位置,在位置发生显著变化后,比如500米。这是工作示例

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

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.

但我被困在的是通过webservice发布位置。

#6


-3  

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

@Jack。是的,你可以在每1小时之后调用这个方法,所以使用NSTimeinterval会很有帮助!

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