2011-11-28 183 views
0

我得到了一些将数据从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; 
} 

回答

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; 
+0

谢谢你的回答。我试过这个代码,但它还没有完成。当它更新数据库时,它检查对象。第一个查询结果总是在数据库中有0个对象。这很奇怪。 – Dawid

+0

你愿意再次发送邮件吗?比我可以发送它。提前致谢。 – Dawid

+0

确定收到你的邮件,你现在可以删除它:)我会尝试一些事情后发送它。提前致谢。 – Dawid

0

我不知道如果与ID不存在物体抓取请求将返回错误。您可能需要考虑使用对象的数量。因此代

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

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

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

+0

谢谢你的回答。我尝试了代码,但它不能正常工作。每次尝试更新数据库时,都会检查对象。总是第一个查询结果与0对象,而他们在数据库中。这很奇怪。 – Dawid

+0

是那个single =在'predicateWithFormat:@“id =%@”'一个错字?我认为这应该是'id ==%@' – Olaf

+0

它确实是一个单一的=但我仍然有同样的怪异的错误。 – Dawid