2011-11-28 151 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