4

当应用程序终止时,我将数据写入数据库。

从 iOS 4 开始,没有调用 applicationWillTerminate:,我在 applicationDidEnterBackground: 和 applicationWillTerminate: 中都放置了相同的保存代码:

我读到 applicationDidEnterBackground: 中执行的任务需要一些时间。

那么,如果在数据库上的写入需要更多,我该如何管理呢?

4

1 回答 1

4

You get 5 seconds to perform any saving etc in the applicationDidEnterBackground: method.

If you need more than 5 seconds there is this UIApplication method to request a background thread which will allow for more time to do whatever you need:

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler

Here is Apple's example code for this:

http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5-SW12

于 2011-01-22T23:10:02.817 回答