Is there a way to limit duplicate entries in master data?

I am trying to add objects to the master data. Therefore, I want it to not allow duplicate entries in the master data store. How to do it? This is my data retention code.

  -(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");

    }


}
+5
source share
5 answers

Since there is no built-in method, you need to get the results and check if the result contains an object that you do not want to duplicate.

Here is the 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 helps you!

+6
source

no, coredata has no built-in uniquing, as it is not a DB.


You need to make sure your program logic is unique.

. , , 1 , , !

== > -, , env

+2

Core Data , , , . . , Core Data , .

, , , NULL , .

,

+1

, countForFetchRequest, , , . - , , . Swift 2.0, NSManagedObject.

  func tokenExists (aToken:String) -> Bool {
        let request: NSFetchRequest = NSFetchRequest(entityName: self.className!)

        let predicate = NSPredicate(format: "token == %@", argumentArray: [aToken])

        request.predicate = predicate

        let error: NSErrorPointer = nil

        let count = self.managedObjectContext!.countForFetchRequest(request, error: error)

        if count == NSNotFound {
            return false
        }
        return true
    }

NB - ( , , , , , , ~ )

:

:

func insertToken (value:String) {

        if  !tokenExists(value) {
            self.token = value
            saveState()
        }

    }
+1

Apple. . "ThreadedCoreData".

https://developer.apple.com/library/content/samplecode/ThreadedCoreData/Introduction/Intro.html

    // before adding the earthquake, first check if there a duplicate in the backing store
    NSError *error = nil;
    Earthquake *earthquake = nil;
    for (earthquake in earthquakes) {
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"location = %@ AND date = %@", earthquake.location, earthquake.date];

        NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        if (fetchedItems.count == 0) {
            // we found no duplicate earthquakes, so insert this new one
            [self.managedObjectContext insertObject:earthquake];
        }
    }
0
source

All Articles