简体   繁体   English

如何防止主线程ios杀死后台线程

[英]how to prevent main thread ios from killing background thread

I have a method in my AppDelegate that saves a NSManagedObjectContext passed as a parameter. 我的AppDelegate中有一个方法可以保存作为参数传递的NSManagedObjectContext This method may be called on either the main thread or a background thread, and I want to ensure that when this method is called the main thread doesn't kill it halfway through when the user terminates the program or what have you. 可以在主线程或后台线程上调用此方法,并且我想确保在调用此方法时,主线程不会在用户终止程序或您拥有程序时中途终止。 How do I prevent the main thread from killing this thread when this method is being executed? 执行此方法时,如何防止主线程杀死该线程?

I also want to do this for writing data to a plist file -- this also may happen outside of the main thread. 我也想这样做,以便将数据写入plist文件-这也可能在主线程之外发生。

I am doing all my background work by way of GCD and dispatch_async 我正在通过GCD和dispatch_async进行所有后台工作

Brad Larson had some suggestions on this post that should help you. 布拉德·拉尔森(Brad Larson) 对此帖子提出了一些建议,应该会对您有所帮助。

In your appDelegate, when your app is getting pushed to the background, it calls applicationDidEnterBackground . 在您的appDelegate中,当您的应用程序被推送到后台时,它将调用applicationDidEnterBackground From this routine, you can see if your method is currently in progress, and can behave accordingly. 从该例程中,您可以查看您的方法当前是否在进行中,并且可以相应地运行。

Depending on how long your task takes to run there are two options: 根据任务运行的时间长短,有两种选择:

  1. If it will take significantly less than 5 seconds, you can just have your applicationDidEnterBackground wait until your task is completed before releasing. 如果花费的时间不到5秒,则可以让applicationDidEnterBackground等到任务完成后再发布。 If you take longer than 5 seconds, your app will just get force-killed. 如果您花费的时间超过5秒钟,您的应用将被强制终止。

  2. If it could be a while, you can try beginBackgroundTaskWithExpirationHandler : to start a background task that finishes off your task. 如果可能会过一会儿,您可以尝试beginBackgroundTaskWithExpirationHandler :启动一个完成任务的后台任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:如何阻止后台线程更新主线程中的UI? - iOS: How to stop a background thread from updating UI in the main thread? 如何防止IOS并发NSOperation在主线程上运行 - How to prevent IOS concurrent NSOperation from running on main thread 无法从iOS6中的后台线程在主线程上调用代码 - Cannot invoke code on the main thread from the background thread in iOS6 在iOS上将消息从后台线程发送到主线程 - Sending messages from background thread to main thread on iOS iOS:如何让后台线程在主线程上重绘视图 - iOS: How to get background thread to redraw view on main thread 从后台线程切换到主线程时如何在 SwiftUI 中编写 iOS 单元测试 - How to write iOS Unit Testing in SwiftUI when switching from background thread to main thread 在后台和主线程ios中执行 - execution in background and main thread ios 如何从iOS主线程上的UIProgressBar更新 - How to update from UIProgressBar on main thread in iOS iOS - Swift:在主线程中从数据库中获取数据,而不是在后台 - iOS - Swift : fetching data from database in main thread, not in background iOS在后台保存主线程NSManagedObjectContext的更改 - iOS saving main thread NSManagedObjectContext changes in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM