CoreData 编辑/覆盖对象 [英] CoreData Edit/Overwrite Object

查看:14
本文介绍了CoreData 编辑/覆盖对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个新项目,一个使用 Core Data 的拆分视图 iPad 应用程序,我想知道如何添加和删除项目相当清楚.如果我要说改变它来保存文本,那么该文本将显示在 UITextView 中,我如何编辑或覆盖 CoreData 中的对象?

I am playing around with a new project, a split view iPad app using Core Data, and I was wondering, as its fairly clear how to add and remove an item. If I was to say alter this to hold text, then that text be shown in a UITextView, how can I edit or overwrite an object in CoreData?

所以用户在 UITextView 中输入他们的笔记,当他们离开时,它会编辑并保存他们当前选择的笔记(表格视图中的对象).

So the user types their note in the UITextView and when they leave that it edits and saves the note (object in the table view) they have currently selected.

感谢您的帮助.

推荐答案

您只需使用 NSFetchRequest 请求现有对象,更改需要更新的任何字段(一个简单的 myObject.propertyName setter 即可这是必需的),然后对数据上下文执行保存操作.

You simply request the existing object using an NSFetchRequest, change whatever fields need to be updated (a simple myObject.propertyName setter is all that's required), and then perform a save action on the data context.

EDIT 添加代码示例.我同意 MCannon 的观点,Core Data 绝对值得一读.

EDIT to add code example. I agree with MCannon, Core Data is definitely worth reading up about.

此代码假设您使用包含 Core Data 内容的模板创建项目,这样您的应用程序委托就有托管对象上下文等.请注意,此处没有错误检查,这只是基本代码.

This code assumes you created the project with a template that includes Core Data stuff, such that your app delegate has a managed object context, etc. Note that there is NO error checking here, this is just basic code.

获取对象

// Retrieve the context
if (managedObjectContext == nil) {
    managedObjectContext = [(YourAppNameAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

// Retrieve the entity from the local store -- much like a table in a database
NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

// Set the predicate -- much like a WHERE statement in a SQL database
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"YourIdentifyingObjectProperty == %@", yourIdentifyingQualifier];
[request setPredicate:predicate];

// Set the sorting -- mandatory, even if you're fetching a single record/object
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourIdentifyingQualifier" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor release]; sortDescriptor = nil;

// Request the data -- NOTE, this assumes only one match, that 
// yourIdentifyingQualifier is unique. It just grabs the first object in the array. 
YourEntityName *thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];
[request release]; request = nil;

更新对象

thisYourEntityName.ExampleNSStringAttributeName = @"The new value";
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date];

保存更改

NSError *error;
[self.managedObjectContext save:&error];

现在您的对象/行已更新.

Now your object/row is updated.

这篇关于CoreData 编辑/覆盖对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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