【问题标题】:storing a composite pattern (tree) in CoreData with Restkit使用 Restkit 在 CoreData 中存储复合模式(树)
【发布时间】: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:

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 并将它们也添加到数据库中?

【问题讨论】:

  • 您的源数据模型并不重要。然而,JSON 是,您必须在我们提供帮助之前证明这一点。
  • 那不是有效的 JSON。您可以控制更改 JSON 吗?是否必须将 JSON 作为嵌套数据?
  • json 已更新。我从 firefox 得到这个 json

标签: ios core-data tree restkit composite


【解决方案1】:

您的问题似乎与RestKit无关,而是对Core Data的误解。

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

所以只要改变模型中的关系就可以了。

【讨论】:

  • 我设计的模型遵循了这个想法:1.每个menuComponent都有一个父级。 2. 只有菜单有子项。 每个菜单都有menuComponents(子项),但每个menuComponent 不一定要有菜单。 (所以孩子的关系没有相反的)。
  • 虽然这看起来合乎逻辑,但它只会给您带来麻烦。拥有通用关系并不意味着您必须使用它。使其通用并在适当的地方使用它...
  • 我听从了你的建议。它有效,但1。现在没有名为 children 的列!!! 并且 2.rest kit 也添加了一个空行! 这是一个镜头:demo.chevereto.com/dLb 你的想法是什么?父项和子项都是可选的,因为某些菜单没有父项,并且所有 menuItem 都没有子项。
  • 不要看 SQLite。查询上下文并记录它返回的内容。菜单项映射应该有一个identificationAttributes。您还有其他响应描述符吗?
  • 我想我应该有 2 个响应描述符,一个用于 menuComponent,一个用于 menuItem。只需查看更新的代码。我认为我的响应描述符有问题。
猜你喜欢
  • 2016-04-26
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多