2014-02-25 29 views
0

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

回答

1

的问题是您取的请求不符合你真的想显示的内容。您不能告诉您的FRC获取这些对象,但只显示具有匹配日期的任何项目的单个行,但它没有这种灵活性。您手动完成了自己的筛选,因此FRC每次更改任何对象时,都需要重新执行controllerDidChangeContent:中的手动筛选。从FRC插入将是没有意义的,因为FRC不知道您正在使用数组的更改版本来备份表视图。请发布您的代码​​,didChangeObject:didChangeContent

说实话,如果您有一个SaleDate实体,它与销售人员有多对多的关系,这听起来会更简单直接。在此视图中,您只需获取SaleDate,然后加载与选定SaleDate关联的任何Sales的详细tableview。这可以防止您必须手动过滤和管理您自己的阵列,FRC已经涵盖了它。这对你想完成的任务来说是一个更好的方法。

+0

是的,我已经有了这些方法。 – BalestraPatrick

+0

确定编辑我的答案不同的方法 –

+0

使用关系听起来更像是要走的路。这是我与Core Data的第一个项目,所以如果我有疑问,我会接受答案并在这里发帖:) – BalestraPatrick