访问NSManagedObject中的属性会导致内存尖峰和崩溃 [英] Accessing a property in NSManagedObject causes memory spike and crash

查看:96
本文介绍了访问NSManagedObject中的属性会导致内存尖峰和崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个使用核心数据存储的iPhone应用程序。所有的NSManagedObject子类都是由xcode根据我的数据模型自动生成的。其中一个类如下所示:

  @interface客户端:NSManagedObject 
{
}

@property(nonatomic,retain)NSNumber * rate;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSString * description;
@property(nonatomic,retain)NSSet * projects;

@end

创建和保存此类的新实例,但是当我尝试访问这样的实例的'description'属性时,程序意外退出。当在Instruments运行时,我可以看到,在崩溃之前,很多内存被快速分配(这可能是为什么应用退出)。



属性访问类似这样:

  self.clientName = [[client.name copy] autorelease]; 
self.clientRate = [[client.rate copy] autorelease];
self.textView.text = client.description; //这是它崩溃的地方

请注意,其他属性(名称和速率)

解决方案

从Apple文档(核心数据编程指南):



请注意,属性名称不能与NSObject或NSManagedObject的任何无参数方法名称相同,例如,你不能给一个属性名称description(见NSPropertyDescription)。



正如jbrennan所说,这应该是你遇到的问题。


I am writing an iPhone app which uses core data for storage. All of my NSManagedObject subclasses has been automatically generated by xcode based on my data model. One of these classes looks like this:

@interface Client :  NSManagedObject  
{
}

@property (nonatomic, retain) NSNumber * rate;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * description;
@property (nonatomic, retain) NSSet* projects;

@end

Creating and saving new instances of this class works just fine, but when I try to access the 'description' property of such an instance, the program unexpectedly quits. When running in Instruments, I can see that just before the crash, a lot of memory is rapidly allocated (which is probably why the app quits).

The code where the property is accessed looks like this:

self.clientName = [[client.name copy] autorelease];
self.clientRate = [[client.rate copy] autorelease];
self.textView.text = client.description; // This is where it crashes

Note that the other properties (name and rate) can be accessed without a problem.

So what have I done wrong?

解决方案

From the Apple documentation (Core Data programming guide):

Note that a property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject, for example, you cannot give a property the name "description" (see NSPropertyDescription).

As noted by jbrennan, this should be causing the issue you are experiencing.

这篇关于访问NSManagedObject中的属性会导致内存尖峰和崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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