IOS Coredata check if attribute is exist or not

2019-09-07 04:18发布

问题:

I am working on coredata based and XML parsing[RSS Feed] based project. I have to check that if attribute value is available or not in coredata ?

For example :-

I have 3 attribute in my datamodel. name,websitename and feedurl. Now i have to parsing the url with user's input. but at the time of parsing i want to check that if URLNAME is available in coredata or not. If available then parsing will done without inserting into coredata and if URL is not in coredata then it will be inserted into coredata.

Here is my try from where i am able to insert the url in core data. but with this method same url is also enters in coredata.

- (void)feedParserDidFinish:(MWFeedParser *)parser {

    [HUD hide:YES];
    //**Coredata inserting value**//
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"I" inManagedObjectContext:context];
    [newDevice setValue:self.Name forKey:@"name"];
    [newDevice setValue:self.WebsiteName forKey:@"websitename"];
    [newDevice setValue:self.Feedlink forKey:@"feedurl"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    //**Parsing url**//

    NSLog(@"Finished Parsing%@", (parser.stopped ? @" (Stopped)" : @""));
    NSArray* array =[parsedItems sortedArrayUsingDescriptors:
                     [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"date"
                                                                          ascending:NO]]];
        NSMutableDictionary* dict = [NSMutableDictionary new];
        [dict setObject:chanelInfo forKey:@"title"];
        [dict setObject:array forKey:@"data"];
        [sectionheaders addObject:dict];
            NSLog(@"SectionHeader Count:%ld",(long)sectionheaders.count);
            [self updateTableWithParsedItems];


}

回答1:

If you want to prevent an object from being inserted if it is already there, you have two basic options.

The first is to check for its existence, and only create the new eject if it is not in the database.

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"I"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"feedurl = %@", self.Feedlink];
if ([[moc executeFetchRequest:fetchRequest error:NULL] count] == 0) {
    // Create a new managed object
}

The second is to use the new unique constraints introduced in iOS9. For details, please see the WWDC 2015 core data presentation.

Note, that if you have lots of objects, you should probably allow the attribute you search on to be indexed, or you will end up doing a linear search for every object.

Also, if you parse lots of objects, you are better off searching for multiple items at once, and/or using a much better algorithm.

The WWDC 2013 "Core Data Performance" presentation has an excellent example of implementing "update-or-insert" that you should study.