我有这两个辅助方法。将 Xcode 中的优化级别设置为无(调试模式的默认设置),代码可以正常工作。但是,如果优化级别设置为 None 以外的任何值,则 testGetAllRecords 中的日志会生成 (null)。
有什么建议为什么它会这样吗?我错过了什么?
(正在使用 ARC)
+(NSArray *)getAllRecords
{
NSError *error;
CoreDataController *coreDataController = [[CoreDataController alloc]init];
NSManagedObjectContext *context = [coreDataController managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group1" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortByName, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Group1 *aGroup in fetchedObjects)
{
// This produces valid data
NSLog(@"getAllRecords name:%@", aGroup.name);
}
NSLog(@"getAllRecords currenthread: %@", [NSThread currentThread]); //shown thread num = 1
return fetchedObjects;
}
+(void)testGetAllRecords
{
NSLog(@"testGetAllRecords currenthread: %@", [NSThread currentThread]); //shown thread num = 1
NSArray *allRecords = [DataStoreInterfaceWrapper getAllRecords];
for (Group1 *aGroup in allRecords)
{
//This produces "(null)" when Xcode Optimization Level not set to None
NSLog(@"testGetAllRecords name:%@", aGroup.name);
}
}