ios - NSFetchedResultsController 获取特定值

标签 ios objective-c core-data nsfetchedresultscontroller

我正在为一家商店开发一个应用程序,您可以在其中登记销售情况并按天订购。我有一个 splitViewController,我正在使用项目附带的模板代码。我有我的 NSFetchedResultsController,它获取我所有的托管对象。这些对象包含销售的所有详细信息(日期、价格、时间、总价)。 我想创建一个包含所有不同日期的数组。这是一个例子。这些是我使用 NSFetchedResultsController 检索的销售额:

25 号星期一

25 号星期一

26 号星期二

星期三 27 日

我想得到一个这样的数组:

25 号星期一

26 号星期二

星期三 27 日

我希望它作为我的主 TableView 的数据源。当用户点击一行时,它会将当天的所有托管对象传递给详细信息 TableView。

问题是我找不到在 fetchedResultsController 中检索它并将其设置为数据源的方法。如果我将数组设置为数据源并添加一天,我会得到:CoreData: error: Serious application error。在调用 -controllerDidChangeContent: 期间从 NSFetchedResultsController 的委托(delegate)捕获了一个异常。尝试将第 4 行插入第 0 节,但在使用 userInfo (null) 更新后第 0 节中只有 2 行

NSFetchedResultsController 的委托(delegate)有问题。这是我的代码:

- (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;
}

最佳答案

问题是您的提取请求与您实际想要显示的内容不匹配。您不能告诉您的 FRC 获取这些对象,但只为任何具有匹配日期的项目显示一行,它根本没有这种灵 active 。您自己手动完成了过滤,因此每当 FRC 更改任何对象时,您都需要在 controllerDidChangeContent: 中重做该手动过滤。从 FRC 插入是没有意义的,因为 FRC 不知道您正在使用数组的更改版本来支持 TableView 。请发布您的 willChangeContent:didChangeObject:didChangeContent 代码。

老实说,如果您有一个与 Sales 有一对多关系的 SaleDate 实体,这听起来会简单得多、直接得多。在此 View 中,您只需获取 SaleDates,并加载包含与所选 SaleDate 关联的任何销售的详细信息 TableView 。这避免了您必须手动过滤和管理您自己的阵列,FRC 涵盖了它。对于您想要实现的目标,这将是一种更好的方法。

关于ios - NSFetchedResultsController 获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021777/

相关文章:

iphone - 用 iPhone 控制蓝牙音频设备

ios - 错误 : Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

ios - 如何从 2 个 NSMutableArray 中比较和删除常见对象(NSDictionaries)?

iphone - 如何在 iPhone 的 Xcode 中创建 Core Data 模型后创建类

objective-c - 设置核心数据对象的标量属性时的 EXC_BAD_ACCESS (IOS 5)

ios - 为特定 ASBD 缓冲区获取正确的数字数据类型转换

ios - 在搜索栏中键入字母时应用程序崩溃

ios - objective c 父类(super class)中的 protected 变量在 swift 子类中不可访问

ios - 如何使用 PJSIP 为 iOS 客户端应用捕获和翻译其他语言的传入音频流?

cocoa - 以编程方式绑定(bind) NSTableView