开启后台任务
This method requests additional background execution time for your app. Call this method when leaving a task unfinished might be detrimental to your app’s user experience. For example, call this method before writing data to a file to prevent the system from suspending your app while the operation is in progress. Do not use this method simply to keep your app running after it moves to the background.
Call this method as early as possible before starting your task, and preferably before your app actually enters the background. The method requests the task assertion for your app asynchronously. If you call this method shortly before your app is due to be suspended, there is a chance that the system might suspend your app before that task assertion is granted. For example, do not call this method at the end of your applicationDidEnterBackground: method and expect your app to continue running. If the system is unable to grant the task assertion, it calls your expiration handler.
Each call to this method must be balanced by a matching call to the endBackgroundTask: method. Apps running background tasks have a finite amount of time in which to run them. (You can find out how much time is available using the backgroundTimeRemaining property.) If you do not call endBackgroundTask: for each task before time expires, the system kills the app. If you provide a block object in the handler parameter, the system calls your handler before time expires to give you a chance to end the task.
You can call this method at any point in your app’s execution. You may also call this method multiple times to mark the beginning of several background tasks that run in parallel. However, each task must be ended separately. You identify a given task using the value returned by this method.
To assist with debugging, this method generates a name for the task that is based on the name of the calling method or function. If you want to specify a custom name, use the beginBackgroundTaskWithName:expirationHandler: method instead.
This method can be safely called on a non-main thread. To extend the execution time of an app extension, use the performExpiringActivityWithReason:usingBlock: method of NSProcessInfo instead.
Use BGProcessingTask for discretionary background processing tasks that require longer periods of time.
--- 翻译 --------
这个方法为你的应用程序请求额外的后台执行时间。当任务未完成时调用这个方法可能会损害你的应用程序的用户体验。例如,在将数据写入文件之前调用此方法,以防止系统在操作过程中挂起应用程序。不要使用这个方法只是为了让你的应用在后台运行。
在开始你的任务之前,尽可能早地调用这个方法,最好是在你的应用程序实际进入后台之前。该方法异步地请求应用程序的任务断言。如果在应用程序挂起前不久调用此方法,则系统可能会在授予任务断言之前挂起应用程序。例如,不要在applicationDidEnterBackground:方法结束时调用此方法,并期望应用程序继续运行。如果系统无法授予任务断言,它将调用过期处理程序。
对该方法的每个调用必须通过对endBackgroundTask:方法的匹配调用来进行平衡。运行后台任务的应用程序运行它们的时间是有限的。(您可以使用backgroundTimeRemaining属性查找可用时间。)如果你没有调用endBackgroundTask:对于每一个在时间到期之前的任务,系统会杀死应用程序。如果你在处理程序参数中提供了一个block对象,系统会在时间到期之前调用你的处理程序,给你一个机会来结束任务。
你可以在应用程序执行的任何时候调用这个方法。您也可以多次调用此方法来标记多个并行运行的后台任务的开始。但是,每个任务必须分别结束。您可以使用此方法返回的值来标识给定的任务。
为了帮助调试,此方法根据调用方法或函数的名称为任务生成一个名称。如果你想指定一个自定义的名字,用beginBackgroundTaskWithName:expirationHandler:方法代替。
可以在非主线程上安全地调用此方法。要延长应用程序扩展的执行时间,请使用performExpiringActivityWithReason:usingBlock: method of NSProcessInfo来代替。
使用BGProcessingTask处理需要较长时间的任意后台处理任务。
-(UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^ __nullable)(void))handler API_AVAILABLE(ios(4.0)) NS_REQUIRES_SUPER;
结束后台任务
You must call this method to end a task that was started using the beginBackgroundTaskWithExpirationHandler: method. If you do not, the system may kill your app
- ----- 翻译 -------
您必须调用此方法来结束使用beginBackgroundTaskWithExpirationHandler:方法启动的任务。如果你不这样做,系统可能会杀死你的应用程序。
可以在非主线程上安全地调用此方法。.
This method can be safely called on a non-main thread.
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)identifier;
网友评论