本文介绍了核心数据NSFetchRequest setResultType:NSDictionaryResultType不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将NSFetchRequest结果类型设置为NSDictinaryResultType时,将返回零个对象。如果我删除setPropertiesToFetch和setResultType,则返回所有对象。

When setting a NSFetchRequest result type to NSDictinaryResultType, zero objects are returned. If I remove setPropertiesToFetch and setResultType, all the objects are returned.

任何想法?

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alert" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];

NSDictionary *entityProperties = [entity propertiesByName]; 
[request setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"test"]]];
[request setResultType:NSDictionaryResultType];

NSError *error;
NSArray *result = [_managedObjectContext executeFetchRequest:request error:&error];

if (result == nil) {
    NSLog(@"Error: %@", [error localizedDescription]);
}


推荐答案

在插入新对象后保存mangedObjectContext。一旦我这样做,我得到的结果,我在寻找。

The problem was I needed to save the mangedObjectContext after inserting new objects. Once I did that I get the results I was looking for.

这篇关于核心数据NSFetchRequest setResultType:NSDictionaryResultType不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:27