【发布时间】:2015-12-19 17:42:30
【问题描述】:
我的核心数据图必须是实体,即 APBook 实体与 APLippingExtract 有一对多的关系。一本书可以有很多剪报。
RootTableViewController,它在部分中显示作者姓名,在行中显示他们的书籍。在 DetailTableViewController 中,从 APClippingExtract 实体中获取数据如下 -
@implementation DetailTableViewController
#pragma mark - fetchedResultController
- (NSFetchedResultsController*) fetchedResultController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"APClippingExtract"];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"extract" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[request setPredicate:[NSPredicate predicateWithFormat:@"extractFromBook == %@", self.currentBook]];
request.fetchBatchSize = 20;
_fetchedResultController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:self.mainContext sectionNameKeyPath:nil cacheName:nil];
//dlegating for NSfetchedResultController
_fetchedResultController.delegate =self;
return _fetchedResultController;
}
当用户删除带有裁剪的行时,下面的方法用于从实体中删除它。
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = self.mainContext;
APClippingExtract *clippingToDelete = [self.fetchedResultController objectAtIndexPath:indexPath];
[context deleteObject:clippingToDelete];
[eeCoreStack saveMainContext];
}
}
EECoreStack是Core Stack setUp的自定义类//保存方法如下
- (void)saveMainContext {
[self.mainContext performBlock:^{
NSError *error = nil;
[self.mainContext save:&error];
if(error){
NSLog(@"ERROR SAVING MAIN MOC %@: %@:", [error localizedDescription], [error userInfo]);
}else {
[self saveMasterContext];
}
}];
}
- (void)saveMasterContext {
[self.masterContext performBlock:^{
NSError *error = nil;
[self.masterContext save:&error];
if(error){
NSLog(@"ERROR SAVING MASTER CONTEXT %@; : %@", [error localizedDescription], [error userInfo]);
}
}];
}
问题是删除行时,返回 RootTableViewController 时,numberOfClipping 计数没有改变。
APBook类中有一个方法
- (void)removeSmallClippings:(NSSet<APClippingExtract *> *)values;
为了上面的工作,需要获取 APBook 属性。因为我目前正在 DetialTableViewController 中获取 APClippingExtract。
如有错误请指正。
编辑 1:
RootTableViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"ListSegue"]){
ListTableViewController *destination = (ListTableViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
APBook *selectedBook = (APBook*)[self.fetchedResultController objectAtIndexPath:indexPath];
destination.currentBook = selectedBook;
destination.mainContext = self.mainContext;
destination.workerContext = self.workerContext;
destination.deleteNow = ^(APClippingExtract *deleteClipping){
[self.mainContext deleteObject:deleteClipping];
deleteClipping.extractFromBook.numberOfClippings = deleteClipping.extractFromBook.numberOfClippings;
[eeCoreStack saveMainContext];
};
}
}
在 DetailTableViewController 中
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
APClippingExtract *clippingToDelete = [self.fetchedResultController objectAtIndexPath:indexPath];
self.deleteNow(clippingToDelete);
}
}
【问题讨论】:
-
我正在使用这种方法从 NSSet 中删除对象,但当我返回 RootTableViewController 时,表格没有更新。
APClippingExtract *clippingToDelete = [self.fetchedResultController objectAtIndexPath:indexPath]; NSMutableSet *mutableSet = [NSMutableSet setWithSet:self.currentBook.smallClippings]; [mutableSet removeObject:clippingToDelete]; self.currentBook.smallClippings = mutableSet; NSLog(@"Sel book clippingis %i", (int)[self.currentBook.smallClippings count]); [eeCoreStack saveMainContext]; -
在重新加载表格视图后,重新加载时,剪报计数正确显示。但是我们不要使用 NSFetchedResultController 这样我们就不必显式地执行 [tableView reloadData] 了。
标签: objective-c core-data