问题描述
我遇到了问题.
周末我一直在做一个项目,我从网络服务中提取一个大的 xml.
Over the weekend I've been working on a project where I'm pulling a large xml from a webservice.
它基本上有 3 层 - 客户、经理、员工,都是分层的.因此,应用程序第一次运行时,它会提取此 xml 并对其进行解析并在 3 个相关实体(客户、经理和员工)中创建所有条目.
It basically has 3 tiers - Clients, Managers, Staff all hierarchical. So the first time the app runs, it pulls this xml and parses it and creates all the entries in the 3 releated Entities - Clients, Managers and Staff.
每次应用程序启动时,我都需要删除相同的 XML,但这一次,我只需要更新"任何已更改的现有记录,或为新客户、经理或员工添加新记录自上次以来出现过.
Every time the app launches I need to pull that same XML down, but this time, I only need to 'update' any of the existing records that have changed, or add new ones for new clients, managers or staff that have appeared since last time.
所以,正如我所说,目前它正在提取所有内容,正确解析并创建正确的实体并填充所有属性.
So - at the moment, as I said, it's pulling it all, parsing it correctly and creating the correct entities and filling in all the attributes.
但是,在没有数据更改的情况下,在第 2 次启动时,它会复制所有数据 - 所以我有 30 个而不是 15 个客户端(正确的数字)等等......
However, with no data change, on the 2nd launch it's DUPLICATING all of the data - so instead of 15 clients ( the correct number ) I have 30 and so on...
我真的必须在解析中添加大量代码来检查它是否已经存在,而不是创建一个新的 NSManagedObject 吗?
Do I really have to add lots of code in my parsing to check that instead of creating a new NSManagedObject, I check if it's already there?
如果是 - 我必须手动检查每个属性?
And if it is - I have to then manually check every attribute?
这非常痛苦和冗长 - 有没有办法让 Core Data 自动为我做这种事情?
That's awfully painful and longwinded - isn't there a way to make Core Data do this kinda stuff for me - automatically?
感谢您的任何帮助或建议.
Thanks for any help or suggestions.
推荐答案
如 Apple Docs 所述 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html
As Stated in Apple Docs https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html
你需要循环数据模型并像这样从那里处理它
You need to loop the data model and handle it from there like this
例子:
// loop over employeeIDs
// anID = ... each employeeID in turn
// within body of loop
NSString *predicateString = [NSString stringWithFormat: @"employeeID == %@", anID];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
我个人不喜欢这种方法,我编写了这段代码,它以高效的方式处理这个问题,而且很简单!我注意到使用 Apples 方法时遇到了字符串具有不同字符(例如大写字母和空格)的问题.如果您正确地重命名所有相应的对象,下面的代码已经过测试并且可以正常工作.老实说,这是实现不在核心数据中添加重复项的最有效方法.
Personally I do not like this method and I wrote this snippet of code that handles this in a pro-efficient manor and which is straight forward! I noticed with Apples method I ran into issues with strings having different characters such as capitol letters and spaces. Below code is tested and working if you rename all your corresponding objects correctly I honestly believe this is the most efficient way to accomplish not adding duplicates in core data.
-(void)AvoidDuplicatesinDataModel
{
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Users"
inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Define how we will sort the records
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"users"
ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
// Fetch the records and handle an error
NSError *Fetcherror;
NSMutableArray *mutableFetchResults = [[managedObjectContext
executeFetchRequest:request error:&Fetcherror] mutableCopy];
if (!mutableFetchResults) {
// Handle the error.
// This is a serious error
}
//here usersNameTextField.text can be any (id) string that you are searching for
if ([[mutableFetchResults valueForKey:@"users"]
containsObject:usernameTextField.text]) {
//Alert user or handle your duplicate methods from here
return;
}
}
这篇关于如何防止 Core Data 在 iOS 5 中重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!