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