在我的应用程序中,我有以下Objective-C代码:
-(void)layoutPages
{
NSMutableArray* sections = [NSMutableArray array];
[sections addObject:[[NSAttributedString alloc] initWithString:@"Hello world"]];
for (NSAttributedString* contentSection in sections) {
NSLog(@"%@",contentSection);
}
}
Run Code Online (Sandbox Code Playgroud)

控制台输出: 2014-04-22 14:11:01.505 MyApp[24784:830b] Hello world{}
如果我使用-Os optimization对x86_64架构进行编译,那么LLVM会默默地优化循环变量'contentSection'.当我使用-O0时,bug就会消失.当我尝试打印contentSection变量的描述时,这是输出:
(lldb) po contentSection
error: Couldn't materialize struct: the variable 'contentSection' has no location, it may have been optimized out
Errored out in Execute, couldn't PrepareToExecuteJITExpression
Run Code Online (Sandbox Code Playgroud)
怎么可能?从我的角度来看,循环变量在循环中使用时永远不应该被优化掉.我已经看到其他人与LLVM有类似的问题,但没有循环变量.这可能是编译器错误吗?
自从将核心数据逻辑迁移到RKManagedObjectStore以来,我遇到了严重的问题.我在视图控制器的主线程中设置了一个NSFetchedResultsController,其上下文设置为[NSManagedObject managedObjectContext]:
assert([NSThread isMainThread]);
NSManagedObjectContext* context = [NSManagedObject managedObjectContext];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:[Item fetchRequest] managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Master"];
Run Code Online (Sandbox Code Playgroud)
我在上下文中插入对象,如下所示:
Item* item = [Item object];
item.name = @"Foo";
[[RKObjectManager sharedManager].objectStore save];
Run Code Online (Sandbox Code Playgroud)
但是获取的结果控制器不会收到有关更改的通知.因此我手动注册了一个通知:
[[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
NSLog(@"Context changed");
[self.fetchedResultsController performFetch:nil];
[self.tableView reloadData];
}];
Run Code Online (Sandbox Code Playgroud)
我认为这应该是不必要的,因为RKManagedObjectStore会在不同的上下文中合并更改.第二,删除我试过的Item对象
[item deleteEntity];
Run Code Online (Sandbox Code Playgroud)
这产生了一个错误,表示无法在另一个上下文中删除该对象.这显然是正确的,但是为什么上下文不是主线程的相同实例?在删除en实体之前,我也在视图控制器内调用以下内容:
assert([NSThread isMainThread]);
NSManagedObjectContext* sameContext1 = [NSManagedObject managedObjectContext];
NSManagedObjectContext* sameContext2 = self.fetchedResultsController.managedObjectContext;
assert(sameContext1 == sameContext2); //FAILS
Run Code Online (Sandbox Code Playgroud)
查看使用[NSManagedObject managedObjectContext]时调用的RKManagedObjectStore的managedObjectContext getter实现,应返回每个线程的相同实例:
-(NSManagedObjectContext*)managedObjectContext {
NSMutableDictionary* threadDictionary = [[NSThread currentThread] threadDictionary]; …Run Code Online (Sandbox Code Playgroud)