核心数据数据库不保存

发布于 2024-09-18 08:02:37 字数 1078 浏览 5 评论 0原文

我正在尝试执行斯坦福大学 CS193 课程中的狗仔队 2 作业,但遇到了问题。我保存数据库的一个调用是当应用程序退出时(我大量借鉴了 Mike Postel 的 版本 检查我的代码):

- (void)applicationWillTerminate:(UIApplication *)application {
    if (flickrContext != nil) {
        if ([flickrContext hasChanges] == YES) {
            NSError *error = nil;
            BOOL isSaved = [flickrContext save:&error];
            NSLog(@"isSaved? %@", (isSaved ? @"YES" :@"NO") );

            // Replace this implementation with code to handle the error appropriately.
            if(isSaved == NO){
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        } 
    }
}

不幸的是,这似乎没有完成这项工作。我偶尔会收到可能与此相关的 EXEC_BAD_ACCESS 调用,但数据库永远不会保存。我已将保存插入到其他部分中,并且在那里工作得很好,只是在这个例程中不行。我不会释放视图中的任何托管对象,只是释放托管对象上下文(flickrContext,或我在视图中调用的任何内容)。

有什么想法吗?

I'm trying to implement the Paparazzi 2 assignment from the Stanford CS193 course and I'm running into a problem. My one call to save the database is when the app exits (I'm borrowing heavily from Mike Postel's version to check my code):

- (void)applicationWillTerminate:(UIApplication *)application {
    if (flickrContext != nil) {
        if ([flickrContext hasChanges] == YES) {
            NSError *error = nil;
            BOOL isSaved = [flickrContext save:&error];
            NSLog(@"isSaved? %@", (isSaved ? @"YES" :@"NO") );

            // Replace this implementation with code to handle the error appropriately.
            if(isSaved == NO){
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        } 
    }
}

Unfortunately, this doesn't seem to be doing the job. I'm getting the occasional EXEC_BAD_ACCESS call that might be related to this, but the database never saves. I've inserted the save into other pieces and it works fine there, just not in this routine. I'm not releasing any of the managed objects in my views, just the managed object context (flickrContext, or whatever I'm calling it in a view).

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

≈。彩虹 2024-09-25 08:02:37

您确定正在调用 applicationWillTerminate: 吗?

有了iOS4和后台进程的支持,现在通常的应用程序生命周期是:

运行->运行。背景->背景暂停-> exit

当转换到后台状态时,您会收到一个 applicationDidEnterBackground: 调用,但当后台进程挂起或退出时,不会收到进一步的通知。

因此,对于 iOS4,您确实需要在 applicationDidEnterBackground: 中保存状态,对于旧版本,则需要在 applicationWillTerminate: 中保存状态

Are you sure that applicationWillTerminate: is even being called?

With iOS4 and background process support, the usual application lifecycle is now:

running -> background -> background suspended -> exit

You get an applicationDidEnterBackground: call when transitioning into the background state, but no further notification when the background process suspends or exits.

So, you really need to save state in applicationDidEnterBackground: for iOS4, as well as in applicationWillTerminate: for older versions

假装不在乎 2024-09-25 08:02:37

flickrContext 是你的 ManagedObjectContext 吗?我敢打赌,当你使用这个方法时,它是零或以其他方式被淹没。您说您要在视图中释放它 - 当然您应该只创建一个视图,让它由应用程序委托拥有,并且仅在应用程序委托的dealloc中释放它?

(当你需要使用它时 -

NSManagedObjectContext* moc = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

关于你的 EXEC_BAD_ACCESS - NSZombieEnabled = YES 会发生什么?静态分析仪说什么?

flickrContext is your managedObjectContext? I'm betting it is nil or otherwise hosed when you hit this method. You say you are releasing it in a view - surely you should be creating just one, having it owned by the application delegate, and release it only in app delegate's dealloc?

(And when you need to use it -

NSManagedObjectContext* moc = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

)

Regarding your EXEC_BAD_ACCESS - what happens with NSZombieEnabled = YES? What does the static analyzer say?

私藏温柔 2024-09-25 08:02:37

打得好。我实际上用老式(暴力)方式解决了这个问题。事实证明 applicationWillTerminate 没有被调用,但这并不明显。创建我从网上借用的数据库的例程是显式释放一个 NSArray,我很确定它是自动释放的。它本质上把这个程序变成了一个定时炸弹。尽管我仍然不明白为什么它会持续这么久,而且直到我试图退出时才显现出来。

我仍在学习 XCode 和 CocoaTouch。我知道 NSZombieEnabled 但我还没有弄清楚如何正确使用它。我还处于笨手笨脚的猴子阶段。不过还是谢谢你的提示。他们很有帮助。

Good call. I actually solved this one the old fashioned (brute force) way. It turns out that applicationWillTerminate wasn't being called, but it wasn't obvious. The routine to create the database that I had borrowed from the web was explicitly releasing an NSArray that I'm pretty sure was autoreleased. It essentially turned the program into a time bomb. Although I still haven't figured out why it lasted as long as it did and just didn't manifest until I tried to exit.

I'm still learning XCode and CocoaTouch. I know about NSZombieEnabled but I haven't figured out how to use it correctly yet. I'm still in the ham-fisted monkey stage. Thanks for the tips, though. They were helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文