核心数据实体在方法中返回后缺少其属性 [英] Core Data entity lacks its attributes after being returned in a method

查看:175
本文介绍了核心数据实体在方法中返回后缺少其属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我有一个方法,应该返回从当前线程的上下文中获取的Core Data实体对象。

I am experiencing an issue where i have a method that is supposed to return a Core Data entity object that is fetched from a context for the current thread.

当我编译和运行项目在DEBUG模式一切正常,但当应用程序运行作为RELEASE一个奇怪的行为发生。返回的对象缺少其属性(它们是nil或0)。

When I compile and run the project in DEBUG-mode all works fine, but when the app is run as RELEASE a strange behavior occurs. The returned object lacks its attributes (they are either nil or 0). The application is using ARC to manage memory.

这个实现使用NSManagedObject上的类别和metod来实现:

The implementation works using a category on NSManagedObject with a metod like this:

- (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。

[NSManaged threadLocalContext] creates a new NSManagedObjectContext for the current thread.

这里的问题是,当NSLogging输出要返回的对象时,一切似乎都很好。关于实体的所有属性和信息都是正确的。

The problem here is that when NSLogging out the object that is about to be returned everything seems fine. All attributes and information about the entity is correct.

但是,当注销对象后返回(如下所示)所有属性都是nil或0.这种行为只发生在RELEASE而不是DEBUG。 / p>

However - when logging out the object AFTER it has been returned (like shown below) all attributes are nil or 0. This behavior only happen in RELEASE and not DEBUG.

Foo * bar = [baz threadLocalSelf];
NSLog(@"Foo object: %@", bar); 

上面的代码导致对象从方法内正确退出,有空属性。虽然ObjectID在两种情况下都是正确的,并且对象不是nil本身。

The above code results in the object being correctly logged out from within the method, but the NSLog right after has empty attributes. While the ObjectID is correct in both cases and the object is not nil itself.

任何关于什么可能导致此问题的想法是非常理解的。

Any ideas as to what could possibly cause this issue is much appriciated.

推荐答案

您的主题新上下文在分配和使用后被释放。

your thread new context is being released after it is allocated and used.

- (id)threadLocalSelf {
    return [self selfInContext:[NSManagedObjectContext threadLocalContext]/*released at the end of method*/];
}

这篇关于核心数据实体在方法中返回后缺少其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆