我在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 !");
}
}