【发布时间】:2014-01-30 18:38:12
【问题描述】:
我在 Visual Studio 中创建了一个复合对象(树结构)并存储在一个 sql server 数据库中。 这是屏幕截图: 现在我想在核心数据中创建这个模型模式,并用 RestKi 存储接收到的数据。
这里是我用 Xcode 设计的:
有人可以帮我用我在 json 中得到的 RestKit 存储这些数据吗?
//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
////
菜单.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,它具有所有菜单和菜单项的所有属性。如上图所示,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:
更新:
这里是 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 并将它们也添加到数据库中?
【问题讨论】:
-
您的源数据模型并不重要。然而,JSON 是,您必须在我们提供帮助之前证明这一点。
-
那不是有效的 JSON。您可以控制更改 JSON 吗?是否必须将 JSON 作为嵌套数据?
-
json 已更新。我从 firefox 得到这个 json
标签: ios core-data tree restkit composite