核心数据中的双重记录

时间:2011-11-28 13:11:02

标签: iphone objective-c ios core-data

我得到了一些代码,可以将数据从rss提要保存到核心数据库。它从数据中创建一个对象。代码应检查该项是否已存在于数据库中。但有时它似乎不起作用。它有时会提取1项双倍。我不知道哪里出错了。

+(NieuwsItem *)NewNieuwsItemWithData:(NSMutableDictionary *)data withContext:(NSManagedObjectContext *)context {

//Method for the creating of an new NieuwsItem object
NieuwsItem *item = nil;

//Create an fetch request to see if item allready is there
NSFetchRequest *request = [[NSFetchRequest alloc] init];

//Set the discriptions for the FetchRequest
request.entity = [NSEntityDescription entityForName:@"NieuwsItem" inManagedObjectContext:context];

request.predicate = [NSPredicate predicateWithFormat:@"id = %@", [data objectForKey:@"id"]];

//Catch the error
NSError *error = nil;

//Excecute the request
item = [[context executeFetchRequest:request error:&error] lastObject];

//If there are no errors and the item is not yet in the database
if (!error && !item ) {

    NSLog(@"Nieuw item aangemaakt met id");

    //Create new agenda item
    item = [NSEntityDescription insertNewObjectForEntityForName:@"NieuwsItem" inManagedObjectContext:context];

    //Create an new date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate *datum = [dateFormat dateFromString:[data objectForKey:@"pubDate"]];

    //And set the item data
    item.naam = [data objectForKey:@"title"];
    item.id = [NSNumber numberWithInt:[[data objectForKey:@"id"] intValue]];
    item.text = [data objectForKey:@"description"];
    item.tumbURL = [data objectForKey:@"tumbafbeelding"];
    item.link = [data objectForKey:@"link"];
    item.datum = datum;

    //Clean
    [dateFormat release];


    //Save the item to the context 
    [context save:nil];

}

//Clean up 
[error release];


//Return the item
return item;
}

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

...

//Catch the error
NSError *error = nil;

[request setFetchLimit:1]; //You shouldn't make a search for more than 1 element, so limit the fetch.

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] 
                                                         initWithFetchRequest:request 
                                                         managedObjectContext:context
                                                         sectionNameKeyPath:nil cacheName:nil];
[fetchedResultsController performFetch:&error];
if (error) {
    // do something
    return nil;
}
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:0];
if ([sectionInfo numberOfObjects] > 0) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    item = [[fetchedResultsController sections] objectAtIndexPath:indexPath];
}
//If there are no errors and the item is not yet in the database
if (!item) {
    ...
}
//Clean up 
[fetchedResultsController release];

//Return the item
return item;

答案 1 :(得分:0)

如果没有id的对象存在,我不确定获取请求是否会返回错误。您可能需要考虑使用对象的计数。因此替换

//Excecute the request item = [[context executeFetchRequest:request error:&error] lastObject];

NSInteger countForFetchRequest = [context countForFetchRequest:fetchRequest error:&error];

如果countForFetchRequest等于零,那么您可以插入对象(具有给定id的对象)作为新对象。