gpt4 book ai didi

ios - 使用 Restkit 在 CoreData 中存储复合模式(树)

转载 作者:行者123 更新时间:2023-11-29 03:17:50 24 4
gpt4 key购买 nike

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

enter image description here enter image description here

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

有人可以帮我用我在 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];

管理对象:菜单组件.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

菜单项.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 之后只添加了一个 super 菜单!!我如何映射 menuItem 并将它们也添加到数据库中?

最佳答案

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

  1. 您的子关系应该位于菜单组件上,并且与父关系相反。这使得 Core Data 能够正确管理关系(如您所期望的那样)。
  2. 您在 SQLite 数据库文件中应该只有 1 个表,因为您使用的是实体继承(这不是问题,只是一个实现细节)。

因此,只需更改模型中的关系即可。

关于ios - 使用 Restkit 在 CoreData 中存储复合模式(树),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com