I have lots of stuff to write upon. Problems that I have faced and how I have solved it. For this post i have picked up coredata topic. Core Data is a persistence framework. I would love to give one beginner’s tutorial on coredata but this post will be about the pitfall that most of the beginners falls into and i have tasted it. I assume that you have already used Core data with your project. I would refer this tutorial for quick basic walkthorough. There was a chat application where i had to manange chat history. I was using Core Data for saving all chat messages. This is how my rough sketch of chat entity looks like:


@interface Chat : NSManagedObject

@property (nonatomic, retain) NSNumber * chat_id;
@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSString * username;


Chat entity has four attributes. Everything was setup properly. When I ran the program, an error occured:

Failed to call designated initializer on NSManagedObject

I was initializing my chat like this:

Chat *chat = [[Chat alloc] init];

According to error init wasn’t appropiate initializer. I checked the class reference docs and according to it:

if you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:)

My chat implementation file looks like this:

#import "Chat.h"

@implementation Chat

@dynamic chat_id;
@dynamic message;
@dynamic timestamp;
@dynamic username;


I looked for initWithEntity in refrence doc and it says:

You are discouraged from overriding this method—you should instead override awakeFromInsert and/or awakeFromFetch (if there is logic common to these methods, it should be factored into a third method which is invoked from both). If you do perform custom initialization in this method, you may cause problems with undo and redo operations.

Then I checked for NSEntityDescription in reference because initWithEntity was taking it as a parameter:

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context

I was going through all the methods in class reference and there I found one method:

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

According to reference docs:

Creates, configures, and returns an instance of the class for the entity with a given name.
Returns a new, autoreleased, fully configured instance of the class for the entity named entityName. The instance has its entity description set and is inserted it into context.

From reading those, I changed my initializer and new code looked like this:

Chat *chat = (Chat *)[NSEntityDescription insertNewObjectForEntityForName:@"Chat" inManagedObjectContext:managedObjectContext];

This worked smoothly for me.