我正在爲一家商店構建應用程序,您可以在其中註冊銷售並按天訂購。我有一個splitViewController,我正在使用該項目附帶的模板代碼。我有我的NSFetchedResultsController獲取我的所有託管對象。這些對象包含銷售的所有細節(日期,價格,時間,totalPrice)。 我想創建一個包含所有不同日子的數組。這是一個例子。這是我與我的NSFetchedResultsController檢索銷售:NSFetchedResultsController獲取特定值
週一25
週一25
週二26日
週三27日
我希望得到一個這樣的數組:
星期一25日
週二26日
週三27日
而且我想這是我的主人的TableView的數據源。當用戶點擊一行時,它會將當天的所有託管對象傳遞給詳細的TableView。
問題是我找不到在fetchedResultsController中檢索它並將其設置爲數據源的方法。如果我將數組設置爲數據源並添加了一天,我會得到:CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to insert row 4 into section 0, but there are only 2 rows in section 0 after the update with userInfo (null)
NSFetchedResultsController的委託存在問題。這裏是我的代碼:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sale" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
_arrayOfDays = [NSMutableArray new];
[[_fetchedResultsController fetchedObjects] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (![_arrayOfDays containsObject:[obj valueForKeyPath:@"date"]]) {
[_arrayOfDays addObject:[obj valueForKeyPath:@"date"]];
}
}];
NSLog(@"%@", _arrayOfDays);
return _fetchedResultsController;
}
在這裏,我numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Number of self.arrayOfDays: %d", (int)_arrayOfDays.count);
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
//return [sectionInfo numberOfObjects];
return self.arrayOfDays.count;
}
是的,我已經有了這些方法。 – BalestraPatrick
確定編輯我的答案不同的方法 –
使用關係聽起來更像是要走的路。這是我與Core Data的第一個項目,所以如果我有疑問,我會接受答案並在這裏發帖:) – BalestraPatrick