我在 OSX 项目的数据模型中保存了一些简单的获取请求模板。(OSx10.9 + Xcode 5.0.2)
如果我创建按钮并单独触发 FR 模板,它们就可以正常工作。如果我获取所有获取请求模板的字典,将所有键名添加到组合框中,然后将 combo.stringValue 与 model.fetchRequestTemplateForName 一起使用,那么只有第一个有效。
我有 NSLog() 语句确认 fetchRequestTemplateForName 正在使用 FR 模板名称提供正确的字符串对象。
示例 - 当为 FR 模板名称提供文字字符串时,所有这些都可以正常工作:
- (IBAction)btnTest:(id)sender {
// get a saved fetch request template from the managed object model
/* // WORKS FINE
NSFetchRequest * fetchRequest =
[self.managedObjectModel
fetchRequestTemplateForName:@"all_cars_under_15000"];
*/
/* // WORKS FINE
NSFetchRequest * fetchRequest =
[self.managedObjectModel
fetchRequestTemplateForName:@"audi_under_20000"];
*/
/* // WORKS FINE
NSFetchRequest * fetchRequest =
[self.managedObjectModel
fetchRequestTemplateForName:@"all_white_cars"];
*/
// WORKS FINE
NSFetchRequest * fetchRequest =
[self.managedObjectModel
fetchRequestTemplateForName:@"all_ford_cars"];
NSError * error = nil;
NSArray * resultsArray =
[self.managedObjectContext
executeFetchRequest:fetchRequest error:&error];
// initialize and open the results window:
if (resultsArray) {
RW =
[[ResultsWindow alloc]
initWithWindowNibName:@"ResultsWindow"];
RW.resultsArray = resultsArray;
[RW showWindow:nil];
}
}
问题代码:
当我将这些模板名称添加到组合框然后使用组合框触发和处理获取请求时,只有一个 FR 模板有效,然后其他模板使用 fetchRequest = nil 轰炸
例子:
-(void) awakeFromNib {
NSDictionary * frDict =
[self.managedObjectModel fetchRequestTemplatesByName];
NSArray * allKeys = frDict.allKeys;
for (int i = 0; i < allKeys.count; i++){
[self.cmbFetchRequests addItemWithObjectValue: allKeys[i]];
}
}
// self.cmbFetchRequests now has all stored FR templates
- (IBAction)cmbChangedStoredFetchRequest:(id)sender {
NSLog(@"%@", self.cmbFetchRequests.stringValue);
NSFetchRequest * fetch =
[self.managedObjectModel
fetchRequestTemplateForName:self.cmbFetchRequests.stringValue];
/* tried this too:
NSFetchRequest * fetch =
[[self.managedObjectModel
fetchRequestTemplateForName:self.cmbFetchRequests.stringValue] copy];
*/
if (fetch) {
// Create a new error object pointer:
NSError * error = nil;
// execute the fetch and deliver results to an array:
NSArray * resultsArray =
[self.managedObjectContext
executeFetchRequest:fetch error:&error];
// initialize and open the results window:
if (resultsArray) {
RW =
[[ResultsWindow alloc]
initWithWindowNibName:@"ResultsWindow"];
RW.resultsArray = resultsArray;
[RW showWindow:nil];
}
} else {
NSLog(@"fetchRequest = nil !");
}
}