我遇到了一个问题,我有一个应该返回一个核心数据实体对象,它是从当前线程的上下文中取出的方法。
当我编译和运行调试模式时,该项目的所有工作正常,但是当应用程序运行作为RELEASE出现奇怪的行为。 返回的对象缺乏其属性(它们或者nil或者0)。 应用程序使用ARC来管理内存。
实施工程,并使用上NSManagedObject类别有这样的梅托德:
- (id)threadLocalSelf {
return [self selfInContext:[NSManagedObjectContext threadLocalContext]];
}
- (id)selfInContext:(NSManagedObjectContext *)context {
NSAssert(context, @"context cannot be nil!");
if(context == self.managedObjectContext)
return self;
NSManagedObjectID *objectID = [self objectID];
if([objectID isTemporaryID])
[NSException raise:NSInternalInconsistencyException format:@"objectID cannot be temporary when fetching self in another context!"];
NSError *error = nil;
NSManagedObject *_self = [context existingObjectWithID:objectID error:&error];
if(error)
[NSException raise:NSInternalInconsistencyException format:@"Failed to fetch self: %@ in context: %@, error: %@", objectID, context, error];
NSAssert(_self, @"context: %@ does not contain an object with objectID: %@", context, objectID);
NSLog(@"Returning _self: %@", _self);
return _self;
}
[NSManaged threadLocalContext]当前线程创建一个新的NSManagedObjectContext。
这里的问题是,NSLogging指出,即将返回的一切对象时,似乎罚款。 所有属性和有关实体的信息是正确的。
然而 - 注销时对象之后已经返回(如下图所示),所有的属性都是零或0此行为仅在RELEASE和DEBUG不会发生。
Foo * bar = [baz threadLocalSelf];
NSLog(@"Foo object: %@", bar);
在对象上面的代码的结果被正确地从所述方法中注销,但所述的NSLog之后具有空属性。 虽然对象ID是在两种情况下正确的对象不是零本身。
任何想法,以什么可能导致此问题大大appriciated。