简体   繁体   English

核心数据中的双重记录

[英]Double records in core-data

I got some code which saves data from a rss feed to a core-data database. 我得到了一些将rss feed中的数据保存到核心数据数据库的代码。 It creates a object from the data. 它根据数据创建一个对象。 The code should check if the item is already in the database. 该代码应检查项目是否已在数据库中。 But sometimes it does not seem to work. 但是有时它似乎不起作用。 It sometimes fetches 1 item double. 有时它会拿来1倍的物品。 I am not sure where it is going wrong. 我不确定哪里出了问题。

+(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;
}

Try to use this code: 尝试使用以下代码:

...

//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;

I am not sure if the fetch request will return an error if no object with id is present. 我不确定如果没有id为id的对象,获取请求是否会返回错误。 You might want to consider using the count of the object. 您可能要考虑使用对象的计数。 Therefore substituting 因此取代

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

with

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

If countForFetchRequest is equal zero then you can insert the object (the one with the given id) as a new object. 如果countForFetchRequest等于零,则可以将对象(具有给定id的对象)插入为新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM