My Core Data图表必须是Entity,APBook Entity与APCLippingExtract有1对多的关系。一本书可以有很多剪报 RootTableViewController,在部分中显示作者姓名,在行中显示他们的书籍。在DetailTableViewController中,数据从APClippingExtract Entity获取,如下所示 -
@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);
}
}
答案 0 :(得分:0)
修改对象(包括删除)不会触发其相关对象的任何更新。这意味着正在观看相关对象的FRC将不会触发。您需要做的是自己修改相关对象。我不知道你拥有什么属性,但这是一个例子。
clipping.book.title = clipping.book.title; // This will trigger FRCs watching book entities.
[context deleteObject:clipping];