使用Restkit在Core Data中存储复合模式(树)

时间:2014-01-30 18:38:13

标签: ios core-data tree restkit composite

我在visual studio中创建了一个复合对象(树结构)并存储在一个sql server数据库中。 这是截图: 现在我想在核心数据中创建这个模型模式,并使用RestKi存储接收到的数据。

enter image description here enter image description here

这里是我用Xcode设计的: enter image description here

有人可以帮我用RestKit存储这些数据吗?我在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];

托管对象: 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

当我浏览创建的sqlite数据库时。我看到只有一个创建的表名menuComponent,它具有所有菜单和menuItem的所有属性。而nhibernate bas为我创建了三个表,如上图所示。 并且所有记录的父字段都为空!!

如何成功存储此数据?请解释。我是RestKit的新手。

我得到的默认JSON:

[{"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"}]

你可以在这里查看json:

http://jsonviewer.stack.hu/

更新

这里是menuComponent RelationShip

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

[menuComponentMapping addPropertyMapping:menuComponentRelationShip];

但我也必须映射menuItem,因为某些属性专门用于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];

但是将此responseDescriptor添加到mobjetManager后只会添加一个超级菜单! 如何映射menuItem并将它们添加到数据库中?

1 个答案:

答案 0 :(得分:1)

您的问题似乎与RestKit根本没有关系,而是对Core Data的误解。

  1. 您的孩子关系应该在菜单组件上,并且与父关系相反。这允许Core Data正确地管理关系(并且如您所愿)。
  2. 您应该只在SQLite数据库文件中有一个表,因为您正在使用实体继承(这不是问题,只是一个实现细节)。
  3. 所以只需改变模型中的关系,你应该好好去。