使用Cocoa和Core Data自动保存 [英] Auto-save with Cocoa and Core Data

查看:126
本文介绍了使用Cocoa和Core Data自动保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用非基于文档的Core Data应用程序。

I am working on a non-document-based Core Data application.

我希望更改在发生时保存。这是用户在这种类型的应用程序中期望的。这也是苹果在iPhoto 或 iTunes 中实现的。

I would like changes to be saved as they happen. This is what the user expects in this type of application. It is also what Apple has implemented in iPhoto or iTunes.

强力的方法是设置定时器经常保存。由保存触发的方法然后将吞下所有验证错误,以免打扰用户。只有退出,用户将被错误排列的数据,以便它可以保存。

A brute force approach would be to set up a timer to save frequently. The method triggered by the saving would then swallow all validation errors so as not to bother the user. Only upon quitting, the user will be bugged to arrange the data so it can save. IMHO, that approach stinks.

所以我想,一定有一种方法,以某种方式钩到保存到像 NSEditor 协议。每次用户(或控制器)完成编辑数据时,应以某种方式通知应用程序委托触发保存操作。这是我不知道在哪里看。

So I am thinking, there must be a way to somehow hook saving to something like the NSEditor protocol. Every time the user (or a controller) finishes editing data, the application delegate should somehow be notified an trigger a save operation. Thing is I don't quite know where to look.

我认为对于更复杂的操作,这可能需要一些交叉验证,我会提出具有位接口的用户绑定到专用 NSManagedObjectContext

I would think that for more complicated operations, which may require some cross-validations to go through, I would present the user with bit of interface tied to a dedicated NSManagedObjectContext.

推荐答案

在AppKit应用程序的每个事件结束时,CoreData将为您运行一个-processPendingTransactions。

At the end of each event in an AppKit app, CoreData will run a -processPendingTransactions for you.

一个副作用是,如果您已经注册NSManagedObjectContext接收更改通知,您将在每个事件结束时调用。

One side-effect of this is that if you've registered with your NSManagedObjectContext to receive change notifications, you'll get called at the end of each event.

因此,例如,在通知处理程序中,您可以调用保存。

So, for instance, in your notification handler, you could call just tell the context to save.

但是,在同一个上下文中的回调中,你可能会对上下文执行保存,所以如果你做了a performSelector:@selector(save :) afterDelay:推送保存,直到-processPendingTransactions完成。

However, you might be paranoid about doing a save on a context while in a callback from that same context, so you'd probably feel better if you did a performSelector:@selector(save:) afterDelay: to push the save until after the -processPendingTransactions is done.

你甚至可以在-save上取消上一个:选择器,并且延迟为5秒,因此如果用户或应用程序处于BUNCH的更改中,它们将合并为一个保存。

You could even do a cancel previous on the -save: selector and have the delay be like 5 seconds, so if the user or app is in the middle of a BUNCH of changes they'll all coalesce into a single save.

事实上,这正是Delicious Library 1.0-1.09的工作原理。

And, in fact, that's exactly how Delicious Library 1.0-1.09 worked.

-Wil

这篇关于使用Cocoa和Core Data自动保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆