/ कोर-डेटा में डबल रिकॉर्ड - आईफोन, ऑब्जेक्टिव-सी, आईओएस, कोर-डेटा

कोर-डेटा में डबल रिकॉर्ड - आईफोन, ऑब्जेक्टिव-सी, आईओएस, कोर-डेटा

मुझे कुछ कोड मिले जो एक आरएसएस फ़ीड से डेटा बचाता हैएक कोर-डेटा डेटाबेस के लिए। यह डेटा से ऑब्जेक्ट बनाता है। यदि आइटम पहले से ही डेटाबेस में है, तो कोड को जांचना चाहिए। लेकिन कभी-कभी यह काम नहीं लगता है। यह कभी-कभी 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 № 1

मुझे यकीन नहीं है कि अगर आईडी के साथ कोई वस्तु मौजूद नहीं है, तो भ्रूण अनुरोध एक त्रुटि लौटाएगा। आप ऑब्जेक्ट की गिनती का उपयोग करने पर विचार कर सकते हैं। इसलिए प्रतिस्थापित कर रहा है

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

साथ में

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

यदि countForFetchRequest बराबर शून्य है तो आप ऑब्जेक्ट (दिए गए आईडी के साथ एक) को एक नई वस्तु के रूप में सम्मिलित कर सकते हैं।


जवाब के लिए 0 № 2

इस कोड का उपयोग करने का प्रयास करें:

...

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