有什么方法可以限制核心数据中的重复条目吗? [英] Is there any way we can restrict duplicate entries in core data?

查看:112
本文介绍了有什么方法可以限制核心数据中的重复条目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在核心数据中添加对象。所以,我想它不应该允许在核心数据存储中的重复条目。怎么做?
这是我的代码与保存数据相关。

   - (IBAction)save: b {

if([name.text isEqualToString:@]&& [address.text isEqualToString:@]&& [phone.text isEqualToString:@] )
{

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Yo ..!
message:@Data Not Saved
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
}
else
{
coreDataAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext * context = [appDelegate managedObjectContext];


NSManagedObject * newContact;
newContact = [NSEntityDescription
insertNewObjectForEntityForName:@Contacts
inManagedObjectContext:context];


[newContact setValue:name.text forKey:@name];
[newContact setValue:address.text forKey:@address];
[newContact setValue:phone.text forKey:@phone];


name.text = @;
address.text = @;
phone.text = @;

NSError * error;
[context save:& error];

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Yo ..!
message:@Data Saved
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];

NSLog(@Object Saved\\\
);

}


}


解决方案

由于没有可用的内置方法,您需要获取结果,并检查结果是否包含您不想重复的对象。



这里是代码片段:

   - (void)checkForDuplicates 
{
NSEntityDescription * entity = [NSEntityDescription entityForName:@Students
inManagedObjectContext:managedObjectContext];

NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@students
ascending:NO];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

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

NSError * Fetcherror;
NSMutableArray * mutableFetchResults = [[managedObjectContext
executeFetchRequest:request error:& Fetcherror] mutableCopy];

if(!mutableFetchResults){
//错误处理代码。
}

if([[mutableFetchResults valueForKey:@users]
containsObject:name.text]){
//通知重复
return ;
}
else
{
//写您的代码以添加数据
}
}
pre>

希望这可以帮助你!


I have been trying to add objects in core data. So, i want that it should not allow duplicate entries in core data store. How to do that? This is my code related to save data.

  -(IBAction)save:(id)sender
    {

        if([name.text isEqualToString:@""] && [address.text isEqualToString:@""] && [phone.text isEqualToString:@""])
        {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yo..!"
                                                        message:@"Data Not Saved"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
            [alert show];
        }
    else
    {
        coreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

        NSManagedObjectContext *context = [appDelegate managedObjectContext];


        NSManagedObject *newContact;
        newContact = [NSEntityDescription
                      insertNewObjectForEntityForName:@"Contacts"
                      inManagedObjectContext:context];


        [newContact setValue:name.text forKey:@"name"];
        [newContact setValue:address.text forKey:@"address"];
        [newContact setValue:phone.text forKey:@"phone"];


        name.text = @"";
        address.text = @"";
        phone.text = @"";

        NSError *error;
        [context save:&error];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yo..!"
                                                    message:@"Data Saved"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
        [alert show];

         NSLog(@"Object Saved\n");

    }


}

解决方案

As there is no built in method available, you need to fetch results and check whether result contains object you don't want to be duplicated.

Here is code snippet:

-(void)checkForDuplicates
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Students"
                                          inManagedObjectContext:managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"students"
                                                               ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

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

    NSError *Fetcherror;
    NSMutableArray *mutableFetchResults = [[managedObjectContext
                                        executeFetchRequest:request error:&Fetcherror] mutableCopy];

   if (!mutableFetchResults) {
        // error handling code.
    }

    if ([[mutableFetchResults valueForKey:@"users"]
         containsObject:name.text]) {
        //notify duplicates
        return;
    }
    else
    {
         //write your code to add data
    }
}

Hope this may help you!

这篇关于有什么方法可以限制核心数据中的重复条目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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