2013-04-27 14 views
0

我在我的應用程序中使用Core Data,只在viewDidLoad中使用此方法一次。當我添加新實體(即按下一個條形按鈕項)時,表視圖數據源方法將一直執行,直到我單擊Xco​​de中的停止按鈕。並且日誌將顯示相同數量的獲取對象(始終爲1)數千次。我把NSlog ... func放在「tableview:nuberOfRowsInSections ..」方法中。所以任何人都可以幫忙弄清楚。謝謝。「self.fetchController performFetch:nil」使表視圖數據源方法一路調用

+0

請爲您提供插入代碼和數據源方法。以及'_fetchController performFetch:nil'是什麼意思? – 2013-04-27 15:19:47

+0

它是NSFetchResultsController的方法: - (BOOL)performFetch:(NSError **)錯誤。 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { info = [self.fetchController.sections objectAtIndex:section]; NSLog(@「count:%d」,[info numberOfObjects]); return [info numberOfObjects]; } – 2013-04-27 16:13:02

+0

您將需要使您的結果控制器代碼和插入代碼可用,以及您的表委託方法(請將它們添加到問題中,以便它們可讀)。這裏有幾個選項,其中一個是您可以一次又一次地執行獲取或創建FRC,或者在每次更新表後更新數據。 – 2013-04-27 16:32:48

回答

0

看着我所看到的來源後:

模板的核心數據項目,主要實體EDLabel
EDLabel聲明的實施文件:

@property (nonatomic, retain) NSDate * labelDate; 
@property (nonatomic, retain) NSString * labelName; 
@property (nonatomic, retain) NSNumber * rowNum; 

該找取請求(經FRC使用):

NSFetchRequest *req = [[NSFetchRequest alloc] init]; 
NSEntityDescription *des = [NSEntityDescription entityForName:@"EDLabel" 
             inManagedObjectContext:self.context]; 
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"labelDate" ascending:NO]; 
req.entity = des; 
req.sortDescriptors = [NSArray arrayWithObject:sort]; 

插入代碼:

-(void)addNewLabel:(id)sender 
{ 
    NSLog(@"%@", NSStringFromSelector(_cmd)); 
    EDLabel *edLabel = (EDLabel *)[NSEntityDescription insertNewObjectForEntityForName:@"EDLabel" 
                   inManagedObjectContext:self.context]; 
    edLabel.labelName = @"New Label"; 
    edLabel.labelDate = [NSDate date]; 
    if(![self.context save:nil]) NSLog(@"Save failed!"); 
} 

有問題的問題是小區配置方法:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    EDLabel *label = [self.fetchController objectAtIndexPath:indexPath]; 
    label.rowNum = [NSNumber numberWithInteger:indexPath.row];//<-- this is the problem 
    //some more configurations 
} 

由於此代碼被稱爲各主要方面進行更新(如果有到FRC的對象的任何變化)的時間,這段代碼修改數據本身(因爲它重置rowNum屬性),我們得到了主循環結束時的一個infinit循環更新(當主要上下文調用processPendingChanges

爲了解決這個問題,要麼刪除更新線(因爲在視圖中的排序是由插入日期完成),或測試,看是否有使用NSNumbercompare:方法在rowNum的變化。

+0

這很清楚,並解決了我的問題,再次感謝,你搖滾~~ – 2013-04-28 17:50:06

相關問題