了解NSManagedObject [英] Understanding NSManagedObject

查看:120
本文介绍了了解NSManagedObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在现有的项目中,我试图在项目创建后很久引入Core Data,因此其模型已经就位。
我创建了xcdatamodel并添加了我唯一的类。
这个类应该作为应用程序中对象的全局存储。
类正确实现了NSManagedObject,并且我已经验证它被创建并保存在上下文中,并且用fetch结果检索。



类是通过NSMutableArray。但这只是不工作。这是这个类的一个片段:

  @interface WZMPersistentStore:NSManagedObject< NSCoding> {
NSMutableArray * persistentStorage;
}

@property(nonatomic,retain)NSMutableArray * persistentStorage;

- (void)add:(id)element;
- (void)remove:(id)元素;
- (id)objectAtIndex:(NSUInteger)index;
- (NSUInteger)num;

@end


$ b $ p在实现中我也重写initWithEntity,

   - (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context {
NSLog init with entity);
[super initWithEntity:entity insertIntoManagedObjectContext:context];

return [self init];
}

init方法只初始化mutable数组,我可以从日志中看到它在创建实体时由应用程序委托正确调用。
add方法只是将消息insertObject发送到persistentStorage。
来自这个问题:


  1. 我在概念上做什么? I
    的意思是,在托管对象中是否有实例
    变量是正确的,而
    初始化就像我一样?

  2. 当ns记录
    的大小时,永久存储器我总是得到0
    ,即使在记录
    之后addObject消息这不是
    true,我已经再次验证,I
    正确地添加了1)

  3. 存储在托管对象
    类中的对象persist persistentStorage是
    具有属性的普通类。是
    有我需要做的事情
    他们吗?我想不是因为我是
    在运行时没有得到任何错误。


解决方案

我终于设法解决这个问题。即使我是objective-c的新手,我认为在项目完成后引入核心数据不是一个好主意。即使许多人声称它很容易。不幸的是,所有的人都这么说,显示一些真正简单的教程的一个实体与一个字符串属性更改。
除了我的项目,我最后写了很多代码,除了现有的,加上一些子类化(例如NSManagedObject),打破了原始模型。这个添加的代码也要仔细写。派生问题可以像属性不保存一样简单,或者像删除错误的实体一样危险。



事实上,我的问题是由于解码和编码方法配置错误对于我的问题:



- 一个仍然没有回答,因为我还没有对objective-c有信心。



- 第二,正如我说的相关对象有一些编码/代码的问题。



- 第三,我错了,有很多代码要写,取决于相关类的复杂程度。


In an existing project I have tried to introduce Core Data long after the project was created, so its model is already in place. I have created the xcdatamodel and added my only class to it. That class should act as a global storage for objects in my application. The class properly implement NSManagedObject and I have verified it gets created and saved in context, also retrieved with a fetch result.

The way of saving data in this class is by means of NSMutableArray. But this is just not working. Here's a fragment of this class:

@interface WZMPersistentStore : NSManagedObject<NSCoding> {
 NSMutableArray *persistentStorage;
}

@property(nonatomic,retain) NSMutableArray *persistentStorage;

-(void)add:(id)element;
-(void)remove:(id)element;
-(id)objectAtIndex:(NSUInteger)index;
-(NSUInteger)num;

@end

In the implementation I also override the initWithEntity like this:

- (id)initWithEntity:(NSEntityDescription*)entity insertIntoManagedObjectContext:(NSManagedObjectContext*)context {
 NSLog(@"init with entity");
 [super initWithEntity:entity insertIntoManagedObjectContext:context];

 return [self init];
}

The init method only initialize the mutable array, and I can see from the log that it gets properly called by the app delegate when creating entity. The add method just send message insertObject to persistentStorage. The questions that come from this:

  1. Am I doing "conceptually" right ? I mean, is it correct to have instance variable in managed object and initialize like I did ?
  2. when ns logging the size of the persistentStorage I always get 0 even when logging a moment after the addObject message (edit: that's not true, I have verified again and I correctly got 1 added).
  3. The object stored in managed object class trough persistentStorage are normal class with attribute. Is there something I need to do with them ? I suppose not because I am not getting any error at runtime.

解决方案

I finally manage to solve this issue. Even if I am a newbie in objective-c, I think that introducing core data after the project is done, is not a good idea. Even if many claim it's easy. Unfortunately, all the people saying so, are showing as proof some really simple tutorial of one entity with one string attribute to change. Instead for my project I ended up writing much code in addition to the existing one, plus some subclassing (NSManagedObject for example) which break the original model. This added code has also to be written carefully. Derived problem can be as simple as an attribute not saved, or dangerous as deleting wrong entities.

Infact, my problem was due to a wrong configuration in decode and encode method in the classes involved in the process of serialization.

For my questions:

-Point one still remain unanswered, because I am not yet confident in objective-c

-Point two, as I said the related object had some problem with encode/code.

-Point three, I was wrong, there's a lot of code to write, depending how complex is the relevant class.

这篇关于了解NSManagedObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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