简体   繁体   English

iOS应用程序进入后台状态时启动线程

[英]Start thread when iOS application enters background state

I have a Requirement where I need to Create and Start a thread when application enters the Background State. 我有一个要求,当应用程序进入后台状态时,需要在其中创建和启动线程。 The function of the Created Thread is to fetch data from local DB and upload to server, and I don't need to do any updates on UI. Created Thread的功能是从本地数据库获取数据并上传到服务器,并且我不需要在UI上进行任何更新。 My questions are: 我的问题是:

  1. Where exactly should I create the Thread - either in applicationWillResignActive method or in applicationDidEnterBackground ? 我到底应该在哪里创建线程-在applicationWillResignActive方法中还是在applicationDidEnterBackground
  2. Which is best way of creating the Thread - way or GCD or Posix Way? 哪种最佳创建线程的方法或GCD或Posix方式?

The things I've tried 我尝试过的东西

  1. I've already worked on Android so I know how can I achieve this scenario, but I'm new to iOS, so I'm getting bit confused to start with. 我已经在Android上工作过,所以我知道如何实现此方案,但是我是iOS的新手,所以一开始我有点困惑。
  2. I just tried the with NSThread concept, but it was not working. 我只是尝试使用NSThread概念,但是它不起作用。 Below is code that I wrote to the best of my knowledge: 以下是据我所知编写的代码:

     - (void)applicationWillResignActive:(UIApplication *)application { [NSThread detachNewThreadSelector:@selector(FetchReportFromDBAndUpload) toTarget:self withObject:nil]; } - (void)FetchReportFromDBAndUpload { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //Check for internet connection and fetch data from DB and upload to server [pool release]; } 

Is this correct way of creating the thread, or do I need to do some changes? 这是创建线程的正确方法,还是我需要进行一些更改? If not please guide me how to achieve this. 如果没有,请指导我如何实现。 Thanks in advance 提前致谢

You must call the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class from the AppDelegate's applicationDidEnterBackground: method. 您必须从AppDelegate的applicationDidEnterBackground:方法调用UIApplication类的beginBackgroundTaskWithExpirationHandler: applicationDidEnterBackground:方法。 This method marks your task as a background task so the system will not kill it when the app enters background state. 此方法将您的任务标记为后台任务,因此当应用程序进入后台状态时,系统不会将其杀死。 (Note that background tasks that are not related to VoiceIP, playing music, GPS tracking are still limited to more or less 10 minutes of background execution time, ie. they don't run forever.) (请注意,与VoiceIP,播放音乐,GPS跟踪无关的后台任务仍被限制为大约10分钟的后台执行时间,即它们不会永远运行。)

Here's more detailed information and a code example: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html 这里是更详细的信息和代码示例: http : //developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

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

相关问题 当iOS应用程序直接进入后台状态时? - When an iOS app directly enters the background state? 当应用程序进入后台时,popToRootViewController - popToRootViewController when application enters background iOS 启动后台线程 - iOS start Background Thread 流星(反应):iOS应用程序进入后台(后台运行实例)时所有图像消失 - Meteor(React) : iOS application when enters to background(background running instance) all images disappears iOS。 每当应用程序从后台进入活动状态时,如何从头开始启动情节提要? - iOS. How to start storyboard from the beginning every time when application goes from background to an active state? 应用程序进入后台时显示启动画面 - Show splash screen when application enters background 当应用程序进入后台时使计时器无效 - Invalidate Timer when application enters background 在iOS中如何在应用程序处于后台状态时重新打开应用程序? - in iOS how to reopen application when application is in background state? 当应用程序进入后台状态时,layoutSubviews 被重复调用 - layoutSubviews getting called repeatedly when the app enters background state 在 iOS 中,当应用程序处于后台或终止 state 时,不会向用户显示 CallKit - In iOS, CallKit is not shown to the user, when application is in background or terminated state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM