我正在为一家商店构建应用程序,您可以在其中注册销售并按天订购。我有一个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