Frage

I have create an composite object (tree structure) in visual studio and stored in an sql server database with. here are the screenshots: now I want to create this model schema in core data and store the received data with RestKi.

enter image description here enter image description here

here what I have designed with Xcode: enter image description here

could someone help me to store this data with RestKit which I get in json ?

//Initialize managed object store
.
.
.

//Complete Core Data stack initialization
.
.
.

RKEntityMapping *menuMapping = [RKEntityMapping mappingForEntityForName:@"Menu" inManagedObjectStore:managedObjectStore];
    menuMapping.identificationAttributes = @[@"menuComponentID"];
    [menuMapping addAttributeMappingsFromDictionary:@{
                                                     @"ID":@"menuComponentID",
                                                     @"Name":@"name",
                                                     @"Description":@"descriptions",
                                                     @"Thumbnail":@"thumbnail",
                                                     @"Image": @"image",

                                                     }];

    RKEntityMapping *menuItemMapping = [RKEntityMapping mappingForEntityForName:@"MenuItem" inManagedObjectStore:managedObjectStore];
    [menuItemMapping addAttributeMappingsFromDictionary:@{
                                                          @"ID" : @"menuComponentID",
                                                          @"Description" : @"descriptions",
                                                          @"Name" : @"name",
                                                          @"Thumbnail":@"thumbnail",
                                                          @"Image":@"image",
                                                          @"Calorie":@"calorie",
                                                          @"Vegeterian":@"vegeterian",
                                                         }];
    RKRelationshipMapping *menuRelationShip = [RKRelationshipMapping relationshipMappingFromKeyPath:@"MenuComponents" toKeyPath:@"children" withMapping:menuMapping];
    [menuMapping addPropertyMapping:menuRelationShip];

    RKResponseDescriptor *menuResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:menuMapping
                                                                                                method:RKRequestMethodGET
                                                                                           pathPattern:@"/api/menu/"
                                                                                               keyPath:nil
                                                                                           statusCodes:[NSIndexSet indexSetWithIndex:200]];
    [objectManager addResponseDescriptor:menuResponseDescriptor];

managed objects: menuComponnet.h

@class MenuComponent;

@interface MenuComponent : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * descriptions;
@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * thumbnail;
@property (nonatomic, retain) NSString * menuComponentID;
@property (nonatomic, retain) MenuComponent *parent;

@end

////

menu.h

@class MenuComponent;

@interface Menu : MenuComponent

@property (nonatomic, retain) NSSet *children;
@end

@interface Menu (CoreDataGeneratedAccessors)

- (void)addChildrenObject:(MenuComponent *)value;
- (void)removeChildrenObject:(MenuComponent *)value;
- (void)addChildren:(NSSet *)values;
- (void)removeChildren:(NSSet *)values;

@end

menuItem.h

@interface MenuItem : MenuComponent

@property (nonatomic, retain) NSNumber * price;
@property (nonatomic, retain) NSNumber * calorie;
@property (nonatomic, retain) NSNumber * vegeterian;

@end

when I browse the created sqlite data base. I see there is only one created table name menuComponent which has all attributes of all menu and and menuItem. while nhibernate bas created for me three table as you see in the above picture. and parent field for all of the records is null!!

how can I successfully store this data? please explain. I'm new to RestKit.

Default JSON I got:

[{"ID":"213028b8-9f59-4d7f-bae9-a2c301815a43","MenuComponents":[{"ID":"d1a861d3-d9e8-41a4-9577-a2c301815a44","MenuComponents":[{"ID":"fa6d172d-ab26-4782-8306-a2c301815a44","Vegetarian":false,"Calorie":0,"Name":"Double Chocolate Cake","Description":"With a melted heart of chocolate","Parent":{"ID":"d1a861d3-d9e8-41a4-9577-a2c301815a44"},"Version":"AAAAAAAAF64="},{"ID":"f9050f6b-8419-4457-9ebd-a2c301815a44","Vegetarian":false,"Calorie":0,"Name":"Tiramisu","Description":"Lady finger cookies are soaked in coffee liqueur and rum","Parent":{"ID":"d1a861d3-d9e8-41a4-9577-a2c301815a44"},"Version":"AAAAAAAAF60="}],"Name":"Dessets Menu","Description":"Desserts for serving after meal","Parent":{"ID":"213028b8-9f59-4d7f-bae9-a2c301815a43"},"Version":"AAAAAAAAF6o="},{"ID":"1f50565a-eeb2-4ab0-ba7b-a2c301815a44","MenuComponents":[{"ID":"0352c5a2-081a-496d-86aa-a2c301815a44","Vegetarian":true,"Calorie":0,"Name":"Grilled Chicken Salad","Description":"Sliced, grilled chicken tops a fresh salad mix, peas","Parent":{"ID":"1f50565a-eeb2-4ab0-ba7b-a2c301815a44"},"Version":"AAAAAAAAF6w="},{"ID":"4fad8aa0-285f-4419-b85e-a2c301815a44","Vegetarian":true,"Calorie":0,"Name":"Carolina Chicken Salad","Description":"Salad mix, fried chicken tenders, peas, cheddar cheese","Parent":{"ID":"1f50565a-eeb2-4ab0-ba7b-a2c301815a44"},"Version":"AAAAAAAAF6s="}],"Name":"Salad & Combinations","Description":"Salad & Combinations","Parent":{"ID":"213028b8-9f59-4d7f-bae9-a2c301815a43"},"Version":"AAAAAAAAF6k="}],"Name":"Lunch Menu","Description":"Lunch Super Menu","Version":"AAAAAAAAF6E="},{"ID":"fa6d172d-ab26-4782-8306-a2c301815a44"},{"ID":"0352c5a2-081a-496d-86aa-a2c301815a44"},{"ID":"d1a861d3-d9e8-41a4-9577-a2c301815a44"},{"ID":"f9050f6b-8419-4457-9ebd-a2c301815a44"},{"ID":"4fad8aa0-285f-4419-b85e-a2c301815a44"},{"ID":"1f50565a-eeb2-4ab0-ba7b-a2c301815a44"}]

you can view the json here:

http://jsonviewer.stack.hu/

UPDATE:

here is menuComponent RelationShip

RKRelationshipMapping *menuComponentRelationShip = [RKRelationshipMapping relationshipMappingFromKeyPath:@"MenuComponents" toKeyPath:@"children" withMapping:menuComponentMapping];

[menuComponentMapping addPropertyMapping:menuComponentRelationShip];

but I must map menuItem too because some property are exclusively for menuItem.

  RKEntityMapping *menuItemMapping = [RKEntityMapping mappingForEntityForName:@"MenuItem" inManagedObjectStore:managedObjectStore];
    [menuItemMapping addAttributeMappingsFromDictionary:@{
                                                          @"ID" : @"menuComponentID",
                                                          @"Description" : @"descriptions",
                                                          @"Name" : @"name",
                                                          @"Thumbnail":@"thumbnail",
                                                          @"Image":@"image",
                                                          @"Prcie":@"price",
                                                          @"Calorie":@"calorie",
                                                          @"Vegeterian":@"vegeterian",
                                                         }];
RKResponseDescriptor *menuItemresponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:menuItemMapping
                                                                                                    method:RKRequestMethodGET
                                                                                               pathPattern:@"/api/menu/"
                                                                                                   keyPath:nil
                                                                                               statusCodes:[NSIndexSet indexSetWithIndex:200]];

[objectManager addResponseDescriptor:menuItemresponseDescriptor];

but after adding this responseDescriptor to mobjetManager only addes one super menu!! How can I map menuItem and add them to database too?

War es hilfreich?

Lösung

Your issue doesn't seem to be related to RestKit at all, rather it is misunderstandings about Core Data.

  1. Your children relationship should be on menu component and the inverse of the parent relationship. This allows Core Data to manage the relationships correctly (and as you expect).
  2. You should only have 1 table in the SQLite DB file because you are using entity inheritance (it isn't a problem, just an implementation detail).

So just change the relationship in the model and you should be good to go.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top