如何防止Core Data在iOS 5中重复? [英] How to prevent Core Data making duplicates in iOS 5?

查看:153
本文介绍了如何防止Core Data在iOS 5中重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题。



在这个周末,我一直在一个项目,我从一个web服务拉一个大的xml。 / p>

它基本上有3层 - 客户,经理,工作人员都分层。因此,第一次运行应用程序时,它会拉取此xml并对其进行解析,并创建3个相关实体 - 客户,管理员和员工中的所有条目。



应用程序启动我需要拉同一个XML,但这一次,我只需要'更新'任何已经更改的现有记录,或添加新的客户,经理或工作人员,自上次以来出现。 / p>

所以 - 正如我所说,它正在拉扯所有,正确解析它并创建正确的实体并填充所有的属性。



然而,没有数据更改,在第二次发射它DUPLICATING所有的数据 - 所以,而不是15客户端(正确的数字)我有30等等...



我真的需要在我的解析中添加大量的代码来检查,而不是创建一个新的NSManagedObject,我检查它是否已经存在?



如果是 - 我必须手动检查每个属性?



这是非常痛苦和longwinded-没有一种方法来使核心数据



感谢您的帮助或建议。

解决方案

如Apple Docs中所述 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html



您需要循环数据模型并从中处理它



示例:

  //循环employeeIDs 
// anID = ...每个employeeID又是
//在循环体内

NSString * predicateString = [NSString stringWithFormat:@ employeeID ==%@,anID];

NSPredicate * predicate = [NSPredicate predicateWithFormat:predicateString];

我个人不喜欢这个方法,我写了一段代码,高效的庄园,这是直接!我注意到苹果方法我遇到了具有不同字符,如国会字母和空格的字符串的问题。如果你正确地重命名所有相应的对象,下面的代码被测试和工作,我相信这是最有效的方式,以完成不添加重复的核心数据。

   - (void)AvoidDuplicatesinDataModel 
{
//定义我们的表/实体使用
NSEntityDescription * entity = [NSEntityDescription entityForName:@Users
inManagedObjectContext :managedObjectContext];

//设置获取请求
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

//定义如何对记录进行排序
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@users
ascending:NO];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];

//获取记录并处理错误
NSError * Fetcherror;
NSMutableArray * mutableFetchResults = [[managedObjectContext
executeFetchRequest:request error:& Fetcherror] mutableCopy];

if(!mutableFetchResults){
//处理错误。
//这是一个严重的错误
}

//这里的usersNameTextField.text可以是你正在搜索的任何(id)字符串
if([ mutableFetchResults valueForKey:@users]
containsObject:usernameTextField.text]){
//提醒用户或处理你的重复方法
return;
}
}


I've run into a problem.

Over the weekend I've been working on a project where I'm pulling a large xml from a webservice.

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.

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.

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...

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?

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.

解决方案

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

Example:

// loop over employeeIDs
// anID = ... each employeeID in turn
// within body of loop

NSString *predicateString = [NSString stringWithFormat: @"employeeID == %@", anID];

NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];

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中重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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