什么时候用CoreData在iOS中显式保存? [英] When to explicitly save with CoreData in iOS?
问题描述
我目前正在使用一个简单的CoreData应用程序的iPad,我不知道当我必须明确保存。我知道有自动保存;然而,我觉得我需要明确保存,以确保我的程序不会崩溃。下面是程序的简要概述:
I'm currently working on a simple CoreData app for iPad and I'm not exactly sure when I have to explicitly save. I know that there is auto-save; however, I feel that I need to explicitly save in order to make sure my program doesn't crash. Here's a brief overview of the program:
〜这是一个表单程序,向用户提供一组文本字段和分段控件来填充。 (想想一个人填写调查,或回答关于他们的姓名,年龄等问题。)
~ It's a form program that presents the user with a set of text fields and segmented controls to fill out. (Think a person filling out a survey, or answering questions about their name, age...etc).
〜任何数量的这些字段都可以填写。它们都不是强制性的。
~ Any number of these fields can be filled out. None of them are mandatory.
〜用户可以随时保存和加载表单,并且可以同时保存多个不同的表单。他们可以删除他们保存的文档
~ A user can save and load their form at any time and they can have multiple different forms saved at once. They can delete their saved documents
〜我目前正在使用核心数据存储此数据,其中一个实体对象表示一个表单。此实体对象包含用户已完成的所有字段及其对每个字段的响应的列表。
~ I'm currently storing this data using core data where one entity object represents a form. This entity object contains a list of all of the fields which the user has completed and their responses to each field.
〜我也有一个NSManagedObject这个实体对象的子类。因此,每次填写一个字段时,我会执行一些操作,例如 currentForm.name = textfield.text
。
~ I also have an NSManagedObject subclass of this entity object. Thus every time a field is filled out I do something such as currentForm.name = textfield.text
.
Ok ,所以我的问题是,我什么时候必须明确保存?如果我编写的代码,如 currentForm.name = textfield.text
核心数据执行立即保存或者我必须明确告诉它这样做(假设我不' t想等待自动保存)?有没有其他时候,我可能要保存?
Ok, so my question is, when do I have to explicitly save? If I write code such as currentForm.name = textfield.text
does core data perform an immediate save or do I have to explicitly tell it to do so (assuming that I don't want to wait for the auto-save)? Are there any other times when I might have to save? Do I have to save after I delete a stored document?
谢谢!
推荐答案
保存
NSError *error;
if (![[AppDelegate managedObjectContext] save:&error])
{
NSLog(@"there was an error in Save:%@",error);
}
我会保存早期和经常或至少在通知didEnterBackground
I would save "early and often" or at least on Notifications for didEnterBackground
这篇关于什么时候用CoreData在iOS中显式保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!