簡體   English   中英

NSFetchedResultsController-我的_sectionNameKeyPath_是_nil_! 為什么添加/刪除時出現此錯誤?

[英]NSFetchedResultsController - My _sectionNameKeyPath_ is _nil_ !! Why am I getting this error when adding / deleting?

NSFetchedResultsController是本節中的典型案例,我正在拔頭發。 我已經將sectionNameKeyPath設置為NIL ,而我沒有任何部分(即1個部分)想要讀取的內容。

代碼如下:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"MyObjectType" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                              initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"accountExpenseTypeCache"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];


    return _fetchedResultsController;  
    }

如您所見,我的sectionNameKeyPath設置為nil,

有時,當我添加或刪除行時,它會起作用。 通常,在嘗試刪除或添加時,我通常會在控制台中遇到嚴重崩潰:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:955
(gdb) continue
2011-03-14 18:00:58.104 MyApplicationTest[5741:207] Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted). with userInfo (null)

怎么了 我在讀取4.0之前的NSFetchedResultsController旨在處理用戶驅動的更新? 我只能假定添加和刪除行被視為用戶驅動更新? 我需要實現這樣的東西嗎?

如何實現CoreData記錄的重新排序?

?? 從我所讀的內容中,雖然我不明白為什么我在節中沒有nil時會遇到這個問題。 為什么說是三個部分?

提前致謝!

tableview中的節數不取決於獲取的結果控制器的sectionNameKeyPath ,而是取決於數據源的numberOfSectionsInTableView:返回的值。

通過在更改數據之前不向tableview發送beginUpdate消息可以觸發您遇到的錯誤。 當數據計數不斷變化時,tableview可能會嘗試重繪自身。

您還必須在tableview的數據源對象中實現提取的結果控制器的委托方法,以便在發生更改時向表發出信號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM