我正在为我的iOS应用程序使用Core Data,在我的获取请求中,我通过某些属性进行获取GROUP。因为我必须使用GROUP BY,结果类型是NSDictionary而不是托管对象。
表视图工作正常,我面临的唯一问题是删除行。它不会删除该行,并且上下文也不会删除该对象。
我做了一些挖掘,发现如果我的结果类型是一个托管对象,那么上下文可以删除该对象,并且表视图行被删除但然后GROUP BY功能丢失,因为它仅在结果类型为一个NSDictionary对象。
任何帮助都将不胜感激。
这是FETCH REQUEST方法(已编辑)
NSError * anyError = nil;
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Merchant" inManagedObjectContext:context];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"premiumActName" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[request setSortDescriptors:descriptors];
NSPropertyDescription *accountDesc = [[entity propertiesByName] objectForKey:@"premiumActName"];
NSExpressionDescription* objectIdDesc = [NSExpressionDescription new];
objectIdDesc.name = @"objectID";
objectIdDesc.expression = [NSExpression expressionForEvaluatedObject];
objectIdDesc.expressionResultType = NSObjectIDAttributeType;
NSArray *propertiesToFetch= @[accountDesc, objectIdDesc];
[request setPropertiesToFetch:propertiesToFetch];
[request setPropertiesToGroupBy:[NSArray arrayWithObjects:accountDesc, nil]];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
NSArray *distinctResults = [context executeFetchRequest:request error:&anyError];
NSLog(@"function=%s", __PRETTY_FUNCTION__);
NSLog(@" result =%@", distinctResults);
if(!_fetchedResultsController)
{
_fetchedResultsController = nil;
}
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
if(![_fetchedResultsController performFetch:&anyError])
{
NSLog(@" Error =%@", anyError);
}
if (![context save:&anyError])
{
// Handle the error.
}
这是表格视图删除行方法(EDITED)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
NSDictionary * dictionary = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObjectID * iod = [dictionary objectForKey:@"objectID"];
NSManagedObject * object = [context objectWithID:iod];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[context deleteObject:object];
// Commit the change.
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Couldn't save: %@", error);
}
}
[self.membersTblView reloadData];
}
这也不会删除对象和表视图行!
答案 0 :(得分:1)
在你的情况下
NSManagedObject * object = [self.fetchedResultsController objectAtIndexPath:indexPath];
并不真正返回托管对象而是字典,因此[context deleteObject:object]
不起作用。
以下可行:
"objectID"
添加到propertiesToFetch
。删除对象:
NSDictionary *dict = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObjectID *oid = [dict objectForKey:@"objectId"];
NSManagedObject *object = [context objectWithID:oid];
[context deleteObject:object];
[context save:&error];
由于带有字典结果类型的提取结果控制器不会自动更新,您还必须调用:
[self.fetchedResultsController performFetch:&error];
[self.membersTblView reloadData];
试。