views:

153

answers:

2

Hi all,

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: taht'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.

thanks

A: 

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.

Leonardo
A: 

No, that is not the "right" approach. You can perform initialization of instance variables in awakeFromFetch. Apple guidelines for NSManagedObject subclasses include the following:

You are also discouraged from overriding initWithEntity:insertIntoManagedObjectContext:, dealloc, or finalize. Changing values in the initWithEntity:insertIntoManagedObjectContext: method will not be noticed by the context and if you are not careful, those changes may not be saved. Most initialization customization should be performed in one of the awake… methods. If you do override initWithEntity:insertIntoManagedObjectContext:, you must make sure you adhere to the requirements set out in the method description [...] (NSManagedObject Class Reference)

To really help, I'd need a deeper understanding of what you're trying to accomplish. Regardless, I strongly suggest combing through Apple's Core Data Programming Guide and sample code before proceeding.

Justin