ios coredata如何检查在coredata中是否存在rssfeed url

时间:2015-10-27 06:09:12

标签: ios objective-c core-data

我想设置如果在coredata中可用rss feedurl然后只进行解析,否则它将存储在coredata中。 我试着参考这个问题实施方法
IOS Coredata check if attribute is exist or not

这是我的尝试:

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

    [HUD hide:YES];

    //**Coredata inserting value**//
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"maindata"];
    NSFetchRequest *fetchrequestforside=[NSFetchRequest fetchRequestWithEntityName:@"anotherdata"];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"urloffeed = %@", self.linkfeed];
    fetchrequestforside.predicate=[NSPredicate predicateWithFormat:@"furloffeed = %@", self.linkfeed];
    NSManagedObjectContext *context = [self managedObjectContext];





    if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) {
        // Create a new managed object
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"maindata" inManagedObjectContext:context];
        [newDevice setValue:self.Name forKey:@"urlname"];
        [newDevice setValue:self.WebsiteName forKey:@"websitename"];
        [newDevice setValue:self.Feedlink forKey:@"urloffeed"];


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

    }

    if ([[context executeFetchRequest:fetchrequestforside error:NULL] count] == 0) {
        // Create a new managed object
               NSManagedObject *newDevice1 = [NSEntityDescription insertNewObjectForEntityForName:@"anotherdata" inManagedObjectContext:context];

        if (self.urlname) {
            [newDevice1 setValue:self.Name forKey:@"urlname"];

        }
        if (self.WebsiteName) {
            [newDevice1 setValue:self.WebsiteName forKey:@"websitename"];

        }
        if (self.urloffeed) {
            [newDevice1 setValue:self.Feedlink forKey:@"urloffeed"];

        }

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

    }

注意: - 我必须将url保存在两个实体中。

1 个答案:

答案 0 :(得分:0)

如果您想要多次根据特定属性的值多次插入实体,那么您可能需要编写辅助方法。

这样的事可能会做你想要的。如果插入了对象,则返回YES。在这种情况下,objectPtr将包含新插入的对象。如果对象已存在,或者尝试执行插入时出错,则返回NOobjectPtr将包含现有对象,或errorPtr将包含错误。

- (BOOL)insertEntity:(NSString*)entity
           withValue:(id)value
              forKey:(NSString*)key
               inMOC:(NSManagedObjectContext*)moc
              object:(NSManagedObject**)objectPtr
               error:(NSError**)errorPtr {
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:entity];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"%K = %@", key, value];
    fetchRequest.fetchLimit = 1;
    BOOL inserted = NO;
    NSArray *result = [moc executeFetchRequest:fetchRequest error:errorPtr];
    if (result) {
        NSManagedObject *object = [result firstObject];
        if (object == nil) {
            object = [NSEntityDescription insertNewObjectForEntityForName:entity
                                                   inManagedObjectContext:moc];
            [object setValue:value forKey:key];
            inserted = YES;
        }
        if (objectPtr) {
            *objectPtr = object;
        }
    } else if (objectPtr) {
        *objectPtr = nil;
    }
    return inserted;
}

然后,您可以这样使用它......

NSManagedObject *object;
NSError *error;
if ([self insertEntity:@"maindata"
             withValue:self.Feedlink
                forKey:@"urloffeed"
                 inMOC:context
                object:&object
                 error:&error]) {
    [object setValue:self.Name forKey:@"urlname"];
    [object setValue:self.WebsiteName forKey:@"websitename"];
}