如何使用核心数据执行插入/更新 [英] How to Perform a Insert/Update With Core Data

查看:139
本文介绍了如何使用核心数据执行插入/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Core Data插入记录和删除记​​录的基本知识;不过,我非常感谢您使用最常用的功能之一 - 插入/更新

I've got the basics of inserting records and deleting records with Core Data; however, I'd appreciate help with one of the most common functions - the insert/update.

基本上,我使用 NSMutableArray arrayWithContentsOfURL 获取包含 mysql 表的行的数组。我现在需要做的是同步我的 CoreData 商店。

Basically, I use NSMutableArray arrayWithContentsOfURL to get an array that contains rows from a mysql table. What I need to do is now sync up my CoreData store.

换句话说,行在我的 CoreData 表,但如果它已经存在,我需要更新的记录与最新的值。此外,如果它存在于Core Data而不是下载的数组中,我需要删除它。

In other words, I need to add every row in the array to my CoreData table but if it already exists I need to update the record with the latest values. Also if it exists in Core Data and not in the downloaded array, I'd need to delete it.

我可能会一起破解这个;

I probably could hack this together; however, I'd like to see how its properly and efficiently done without memory leaks.

推荐答案

有两种方法可以插入数据到核心数据 - 和任何一个你使用取决于你。但是,其中一个取决于您是否为Core Data db的数据模型生成了Model类。

There are two ways to insert data into Core Data - and whichever one you use is up to you. However, one of them depends on whether you have generated Model classes for your data model for the Core Data db.

常规方法是使用以下命令:

The regular way is to use the following:

NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
inManagedObjectContext:context];
[object setValue:@"value1" forKey:@"stringColumn"];
[object setValue:12 forKey:@"numberValue"];
NSError *error;
if (![context save:&error]) {
    NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}

这是假设您已经设置了托管对象上下文。
如果在循环中创建并插入对象到上下文中,然后在循环结束后保存,就会更有效。

This is assuming you've already got your managed object context set up. It is much more efficient if you create and insert your objects into the context in a loop, and then save after the loop ends.

另一种方法是'有很大的不同,但在类型安全方面更安全。如果你已经生成了模型类(你可以从
xcdatamodels),那么你可以简单地创建一个类的对象并设置它的属性。

The other method isn't much different, but is much safer in terms of type safety. If you have generated model classes (which you can do from the xcdatamodels) then you can simply create an object of that class and set its properties.

TableObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table" 
inManagedObjectContext:context];
[object setStringColumn:@"value1"];
[object setNumberValue:12];
NSError *error;
if (![context save:&error]) {
    NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}

要从表中删除,只需从表中检索对象m假设你在这里使用第二个方法插入,并且因此生成了模型类)并使用以下:

To delete from a table, simply retrieve the object from the table (I'm assuming you are using the second method here for insertions, and as such have generated model classes) and use the following:

[context deleteObject:object];

请注意,您需要调用save才能生效。

Note that you will need to call save for that to take effect as well.

希望这有助于!祝你好运!

Hope this helps! Good luck!

编辑:
对不起,我必须误解这个问题!

Sorry, I must've misread the question!

检查现有记录,您将需要创建Fetch请求,然后在受管对象上下文中执行它。
至少,获取请求需要一个实体(因此它知道要搜索的表)。
要指定搜索词,您需要创建一个谓词(否则请求将简单地返回表中的所有内容)。您还可以指定一组排序描述符,以便对结果进行排序。

To examine an existing record, you'll want to create a Fetch Request, and then execute it on your managed object context. At bare minimum, a Fetch Request requires an entity (so it knows what table to search on). To specify search terms, you will need to create a predicate (otherwise the request will simply return everything in the table). You can also specify a set of sort descriptors so that your results will be sorted.

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"table" inManagedObjectContext:context];
[request setEntity:entity];

NSError *errorFetch = nil;
NSArray *array = [context executeFetchRequest:request error:&errorFetch];

此代码创建一个获取请求,并从数组中名为table的表中返回每个对象。
从这里,由于所有必需的对象都在数组中,您可以检查和编辑记录。如果您进行任何更改,请记住保存上下文!
以下循环使用与上述示例相同的表格记录每个对象中的第一个值。

This code creates a fetch request, and returns every object from the table named "table" in an array. From here, since all of the required objects are in the array, you can inspect and edit the records. If you make any changes, remember to save the context! The following loop logs the first value in each object, using the same table as the above examples.

for(TableObject *object in array)
{
    NSLog(@"object value1 = %@", object.value1);
}

您也可以使用上述函数从这一点删除记录。

You can also delete records from this point as well using the above mentioned function.

有关获取请求的详情,请提供类参考。我也强烈建议阅读关于排序描述符和谓词,因为它们对于搜索您的核心数据数据库非常重要,并且它们的某些用途比其他用户效率更低(特别是在创建NSPredicates时)。

For more information about Fetch Requests, please give the class reference a look. I would also highly recommend reading about sort descriptors and predicates, since they're very important for searching your Core Data db, and certain uses of them are less efficient than others (particularly in the creation of NSPredicates).

祝你好运!

这篇关于如何使用核心数据执行插入/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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