gpt4 book ai didi

iphone - 当用户退出应用程序时保存核心数据?

转载 作者:可可西里 更新时间:2023-11-01 05:56:09 29 4
gpt4 key购买 nike

我正在尝试在用户按下主页按钮时保存核心数据。我想从 -applicationDidEnterBackground: 执行此操作,但它没有被调用。这可能是因为您无法从此处保存核心数据,或者只是因为没有足够的时间来完成操作。但是,它确实可以从 -applicationWillResignActive: 开始工作,我只是好奇在其中执行此操作的最佳委托(delegate)方法是什么,以及为什么 -applicationDidEnterBackground: 不起作用。(正如您从输出中看到的,这两种方法都被调用了,如果我从 -applicationWillResignActive: 中删除保存,则什么也不会发生,所以这不是一个阻塞另一个的情况)

// CALLS SAVE
- (void)applicationWillResignActive:(UIApplication *)application {
[[self model] saveCoreData:@"SAVE FROM: applicationWillResignActive"];
NSLog(@"APPDEL: %s", __PRETTY_FUNCTION__);
}

// DOES NOT CALL SAVE
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[self model] saveCoreData:@"SAVE FROM: applicationDidEnterBackground"];
NSLog(@"APPDEL: %s", __PRETTY_FUNCTION__);
}

.

// CONSOLE OUTPUT
2013-03-21 cvb[6724:907] APPDEL: -[AppDelegate applicationWillResignActive:]
2013-03-21 cvb[6724:907] APPDEL: -[AppDelegate applicationDidEnterBackground:]
2013-03-21 cvb[6724:907]
2013-03-21 cvb[6724:907] SAVE: SAVE FROM: applicationWillResignActive
2013-03-21 cvb[6724:907] SAVE: Saving successful ...

编辑:

这就是我最后所做的:

- (void)applicationDidEnterBackground:(UIApplication *)application {
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
[[self model] saveCoreData:@"[ENTERING BACKGROUND]"];
}

最佳答案

查看 UIApplicationDelegate protocol reference .

基本上,这是您可能对 applicationDidEnterBackground:

感兴趣的部分

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

关于iphone - 当用户退出应用程序时保存核心数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547877/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com