Creating instance for NSManagedObject Class using Restkit

how can i create an instance for NSmanagedObject Class which i am created using core data model. I have another class which is the subclass of NSObject. I want to create an object for NSManageObject Class. how can i do that. Is it like the normal way that we create the instance for NSobject class like

ClassB.m //ClassB is NSObject Class

ClassA *obj = [[ClassA alloc]init]; //ClassA is NSmanagedObject Class

or is there any way to do that???

i am not using this code(Apple Docs) for creating instance

NSManagedObject *newEmployee = [[NSManagedObject alloc]

            initWithEntity:employeeEntity

            insertIntoManagedObjectContext:context];

i would like to know, is there any way to create an object similar to the above code using restkit

Note: i am using Restkit for creating object instance and mapping.

Thanks


Using Restkit you can use the static method object on the class you want to create. In your example

[ClassA object]

would return you an instantiated object.

Just be sure to import the correct headers:

#import <RestKit/CoreData.h>

instead of

#import <CoreData/CoreData.h>


No, you don't want to instantiate an NSManagedObject via alloc/init. You should use NSEntityDescription 's insertNewObjectForEntityForName:inManagedObjectContext: . Something like:

ClassA *obj = [NSEntityDescription
    insertNewObjectForEntityForName:@"ClassA"
    inManagedObjectContext:context]; // ClassA is NSManagedObject Class

In order to have a NSManagedObjectContext , you also need an NSPersistentStoreCoordinator , and an NSManagedObjectModel , etc. Yeah, it's complex. This book really helped me get my head around Core Data; I recommend it wholeheartedly.

See the Core Data Programming Guide section on creating and deleting managed objects. Actually, while you're there, I'd recommend reading the entire Core Data Programming Guide. Core Data is amazing and powerful, but it is complex; you really want to know what you're doing.

Update : The fact that you're using RestKit might change what I said above. It may be that RestKit has it's own API for doing Core Data stuff, I'm not sure. Maybe check the docs.

链接地址: http://www.djcxy.com/p/49192.html

上一篇: 有人可以解释映射Restkit获取结果映射到数组

下一篇: 使用Restkit为NSManagedObject类创建实例