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

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

问题描述

我正在玩一个新项目,一个使用核心数据的拆分视图iPad应用程序,我想知道,如何添加和删除项目相当清楚。如果我说要修改保存文本,那么该文本将显示在 UITextView 中,如何编辑或覆盖 CoreData



因此,用户在 UITextView 中键入他们的注释,当他们离开时,它编辑并保存注释

解决方案



/ div>

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



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



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



获取对象 p>

  //检索上下文
if(managedObjectContext == nil){
managedObjectContext = [(YourAppNameAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
}

//从本地存储中检索实体 - 很像数据库中的表
NSEntityDescription * entity = [NSEntityDescription entityForName:@YourEntityNameinManagedObjectContext:managedObjectContext ];
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

//设置谓词 - 很像SQL数据库中的WHERE语句
NSPredicate * predicate = [NSPredicate predicateWithFormat:@YourIdentifyingObjectProperty ==%@,yourIdentifyingQualifier];
[request setPredicate:predicate];

//设置排序 - 即使您正在获取单个记录/对象,也是必需的
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@yourIdentifyingQualifier ];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor release]; sortDescriptor = nil;

//请求数据 - 注意,这里假设只有一个匹配,
// yourIdentifyingQualifier是唯一的。它只抓取数组中的第一个对象。
YourEntityName * thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:& error] objectAtIndex:0];
[request release]; request = nil;

更新对象

  thisYourEntityName.ExampleNSStringAttributeName = @新值; 
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date];

保存更改

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

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


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?

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.

Appreciate any help thanks.

解决方案

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 to add code example. I agree with MCannon, Core Data is definitely worth reading up about.

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.

Fetching the object

// 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;

Update the object

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

Save the change

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

Now your object/row is updated.

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

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