1
我正在使用核心数据为我的iOS应用程序,并在我的提取请求中,我通过一些属性做一个获取GROUP。因为我必须GROUP BY,结果类型是NSDictionary而不是托管对象。NSManaged对象上下文无法删除对象
表视图工作正常,我面临的唯一问题是删除行。它不会删除该行,并且该上下文不会删除该对象。
我做了一些挖掘,发现如果我的结果类型是一个托管对象,那么上下文可以删除对象,并且表视图行被删除,但是随后GROUP BY功能会丢失,因为它只在结果类型是一个NSDictionary对象。
任何帮助,将不胜感激。
这是获取请求方法(编者)
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.
}
这表视图删除行法(编者)
- (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];
}
这不会删除对象和表视图行呢!
我试过你说的。它没有工作。 – 2013-03-22 17:55:12
@AngadManchanda:你能否验证'oid'是一个托管对象ID,而'object'是一个托管对象? – 2013-03-22 17:56:18
它实际上幕后工作,当我按删除,它什么都不做,当我做视图会出现或视图加载(重新启动应用程序),我看到该行被删除。所以我错过了一些代码行? – 2013-03-22 17:57:41