我正在从 AppDelegate 调用以下方法。获取请求在 iPad 设备上返回 0 条记录,请告知。记录在模拟器上正确返回。我已经确认在设备和模拟器上传递的参数都不是零。
+ (Movie * ) movieWithID : (NSString * ) ID withObjectContext:(NSManagedObjectContext *) context
{
Movie *movie= nil;
NSFetchRequest *request =[[NSFetchRequest alloc]init];
NSLog(@"ID : %@ Context : %@", ID,context);
// Context and ID are not nil on both simulator and device
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movie" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error =nil;
NSArray * fetchResults = [context executeFetchRequest:request error:&error];
//0 records are returned on the device
//Correct number of records are returned on the simulator
if (entity && request) {
NSLog(@"Fetch Count %d",[fetchResults count]);
}
else
{
NSLog(@"Entity is nil");
}
request.predicate = [NSPredicate predicateWithFormat:@"id like %@",ID];
fetchResults = [context executeFetchRequest:request error:&error];
if ([fetchResults count] > 1) {
NSLog(@"Fetched Results > 1");
}
if (!fetchResults ) {
NSLog(@"Fetch Failed");
}
else if ([fetchResults count]== 0) {
NSLog(@"No movie results %@",error);
}
else if ([fetchResults count] ==1) {
movie= [fetchResults lastObject];
}
return movie;
}