0

I'm having class that inherits NSManagedObject that was generated using my db model:

// .h
@interface Sketch : NSManagedObject

@property (nonatomic, retain) NSDate * added;
@property (nonatomic, retain) NSString * board;
@property (nonatomic, retain) NSString * filepath;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * filename;

@end

// .m
@implementation Sketch

@dynamic added;
@dynamic board;
@dynamic filepath;
@dynamic title;
@dynamic filename;

@end

I'm using that class instances in UITableView. Now I need to add some instances that are not stored in db (just to show them in the list):

Sketch sketch = [[Sketch alloc] init];

But when trying to set instance properties

sketch.title = @"test title";

I'm getting exception:

-[Sketch setTitle:]: unrecognized selector sent to instance 0x7ff112c13e30

Does it mean I have to create instance by adding them to Managed Context only (even if I'm not going to store them)?

[NSEntityDescription insertNewObjectForEntityForName:SKETCH_ENTITY
                                     inManagedObjectContext:context];
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
4ntoine
  • 19,816
  • 21
  • 96
  • 220

1 Answers1

0

No, you can create instances of NSManagedObject subclasses and add them to the managed object context later (while I'd suggest not do do so). You have some issue with your Sketch object, not with NSManagedObject and NSManagedObjectContext.

The only thing is that you should create it like this:

NSManagedObjectContext *moc = ... // your managed object context
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sketch"
                                          inManagedObjectContext:moc];
// note nil for context
Sketch *unassociatedObject =
    (Sketch *)[[NSManagedObject alloc] initWithEntity:entity
                       insertIntoManagedObjectContext:nil];

For more details see this answer.

Community
  • 1
  • 1
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • i'm getting exception when trying to set value for the property for the instance created with `alloc] init]` . As far as i understand setter is generated when adding to managed context so i'm afraid i can't just create object and set property value as with any other class. I don't need to add them to managed context since i need them for uitableview only – 4ntoine Apr 29 '15 at 12:12
  • I don't need to insert it in managed context (`insertIntoManagedObjectContext `) since some other operations can be performed with db and instance will be stored unintentionally. In fact you create instance by adding to managed context instead from the beginning which is undesirable – 4ntoine Apr 29 '15 at 12:16
  • Then don't add it to context at all. The only thing is why this can be even necessary? If you need this, I think, something is wrong with your architecture. – MANIAK_dobrii Apr 29 '15 at 12:18
  • moc is your managed object context, nil goes only to initWithEntity:insertIntoManagedObjectContext: – MANIAK_dobrii Apr 29 '15 at 12:27
  • That's why I wrote about architecture. I'd won't use that in a production app. There could be some issues, and as suggested by Apple engineers it's safer to use some plain NSObjects. I personally usually have some protocols and pair of objects which conform to it. – MANIAK_dobrii Apr 29 '15 at 12:34
  • agree. if it can't be used as regular NSObject i would create model protocol and use it implementors (entity or just display model) as list instances. – 4ntoine Apr 29 '15 at 12:43