核心数据属性更改为nil(ARC相关?) [英] Core Data attribute changes to nil (ARC related?)

查看:136
本文介绍了核心数据属性更改为nil(ARC相关?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些核心数据功能,工作正常,直到一些最近(看似无关)的更改。现在我遇到问题,属于一个特定的NSManagedObject子类实例的所有属性突然返回nil。



让我们说我的NSManagedObject子类叫Foo,它只有一个属性称为值。一旦我意识到价值在某种程度上变成零,我去设置以下类别来监视价值的变化。

  @implementation Foo )

- (void)setValue:(NSDate *)value
{
[self willChangeValueForKey:@value];
[self setPrimitiveValue:value forKey:@value];
[self didChangeValueForKey:@value];
}

- (NSDate *)value
{
[self willAccessValueForKey:@value];
NSDate * value = [self primitiveValueForKey:@value];
[self didAccessValueForKey:@value];

返回值;
}

@end

setValue:对象和传入的参数是一个非零的NSDate。然后检索该值(在另一种方法中)。



但是当另一个方法尝试读取值时,将调用值存取器,并通过primitiveValueForKey返回nil值。

在两次读取之间,setValue:不被调用,Foo对象本身仍然有效(非nil)。事实上,在对任何Core Data对象或整个上下文的两次读取之间不执行其他Core Data操作。



我们在项目中使用ARC。是不是可能ARC不知何故弄乱我的Core Data变量和释放它们?如果有的话,任何人都有任何建议调试ARC的解除分配?或者更好的是,有没有人知道一种方法来确保ARC不会释放我的变量。



这可能不是ARC相关的,但我有一点对于发生了什么的损失。

解决方案

这很有可能是因为 NSManagedObjectContext 这些对象属于,正在走开。当你有 NSManagedObject 实例,但是你不是自己持有上下文,这些托管对象将开始返回 nil



在ARC下,请确保将上下文存储在 strong 变量中, weak static 全局。



,即保留释放代码,请确保您保留上下文。


I have some Core Data functionality that was working fine until some recent (seemingly unrelated) changes were made. Now I'm getting problems where all the attributes belonging to a particular NSManagedObject subclass instance are suddenly returning nil.

Let's say my NSManagedObject subclass is called Foo and it has only one attribute called value. Once I realised value was somehow becoming nil I went and setup the following category to monitor changes to value.

@implementation Foo (Debug)

- (void)setValue:(NSDate *)value
{
    [self willChangeValueForKey:@"value"];
    [self setPrimitiveValue:value forKey:@"value"];     
    [self didChangeValueForKey:@"value"];
}

- (NSDate *)value
{
    [self willAccessValueForKey:@"value"];
    NSDate *value = [self primitiveValueForKey:@"value"];
    [self didAccessValueForKey:@"value"];

    return value;
}

@end

setValue: is called for my object and the argument passed in is a non-nil NSDate. Then the value is retrieved (in another method). The same value that was specified is retrieved correctly.

However when another method tries to read value, the value accessor is called and a nil value is returned by primitiveValueForKey:.

In between the two reads setValue: is not called and the Foo object itself is still valid (non-nil). In fact no other Core Data operations are performed between the two reads on any Core Data object or the context as a whole.

We're using ARC in our project. Is it possible ARC is somehow messing with my Core Data variables and deallocating them? If so does anybody have any suggestions for debugging ARC deallocations? Or better yet, does anyone know a way to ensure ARC doesn't deallocate my variable.

This may not even be ARC related, however I'm at a bit of a loss as to what is going on. Any suggestions would be very much appreciated.

解决方案

This is very likely because the NSManagedObjectContext that these objects belong to, is going away. When you have NSManagedObject instances around but you're not holding on to the context yourself, those managed objects will start returning nil.

Under ARC, make sure you store the context in a strong variable, i.e. an instance variable that's not weak or a static global.

Non-ARC, i.e. retain-release code, make sure you're retaining the context.

这篇关于核心数据属性更改为nil(ARC相关?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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