gpt4 book ai didi

ios - RestKit 与主键的关系映射

转载 作者:可可西里 更新时间:2023-11-01 03:13:25 25 4
gpt4 key购买 nike

当 JSON 响应仅包含主键而不包含用于创建新对象的完全嵌套数组时,我在映射关系时遇到了问题。

我有 2 个类 - Shop 和 Item,正如您所期望的那样,Shop->Item 具有一对多关系。

我有一个商店(和商品)的本地核心数据存储,每个商店都有一个主键。然后我希望下载一个项目列表作为 JSON 并映射到核心数据实体,但只包括商店的主键,而不是所有商店详细信息作为嵌套数组 - 这将是网络流量的巨大浪费,因为我正在下载 500 多个项目的详细信息。

这是来自两个请求的 JSON:

/shops

{
"id" : 1,
"shop" : "Shop A",
"city" : "New York"
},
{
"id" : 2,
"shop" : "Shop B",
"city" : "London"
},
...

/items

{
"id" : 1,
"name" : "Shoes",
"manufacturer" : "Vans",
"shopId" : 1
},
{
"id" : 2,
"name" : "T-shirt",
"manufacturer" : "Animal",
"shopId" : 2
},
{
"id" : 3,
"name" : "Scarf",
"manufacturer" : "Ted Baker",
"shopId" : 1
},
{
"id" : 4,
"name" : "Sunglasses",
"manufacturer" : "Ray-Ban",
"shopId" : 3
},
...

这是我目前的代码。

AppDelegate.m

...

NSURL *baseURL = [NSURL URLWithString:@"http://localhost/company/API"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

[objectManager.HTTPClient setDefaultHeader:@"Accept" value:@"application/json"];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;

// Shop Mapping

RKEntityMapping *shopMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Shop class])
inManagedObjectStore:objectManager.managedObjectStore];
NSDictionary *shopMappingAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"objectId",@"id",@"name",@"shop",@"city",@"city",nil];
shopMapping.identificationAttributes = @[@"objectId"];
[shopMapping addAttributeMappingsFromDictionary:shopMappingAttributes];
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:shopMapping
pathPattern:@"/shops"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];


// Item Mapping

RKEntityMapping *itemMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Item class])
inManagedObjectStore:objectManager.managedObjectStore];
NSDictionary *itemMappingAttributes = [NSDictionary dictionaryWithObjectsAndKeys:@"objectId",@"id",@"name", @"name",@"manufacturer",@"manufacturer",nil];
itemMapping.identificationAttributes = @[@"objectId"];
[itemMapping addAttributeMappingsFromDictionary:itemMappingAttributes];

// Define the relationship mapping

[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:itemMapping
pathPattern:@"/items"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

...

ItemsTableViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

// Update Shops
[[RKObjectManager sharedManager] getObjectsAtPath:@"/shops"
parameters:nil
success:nil
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@",error);
}];

// Update/Get Items
NSDictionary *parameters = @{
@"username": self.username,
@"password": self.password,
@"API_key": @"abc123",
};

NSMutableURLRequest *request = [[RKObjectManager sharedManager] requestWithObject:nil
method:RKRequestMethodPOST
path:@"/items"
parameters:parameters];

RKManagedObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
Item *item = [mappingResult firstObject];
NSLog(@"Mapped the Item: %@", item);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@",error);
}];
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];
}

编辑:Wain,我在应用程序委托(delegate)的相关位置有这个但是得到了一个 NSException

NSEntityDescription *itemEntity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];
NSRelationshipDescription *shopRelationship = [itemEntity relationshipsByName][@"shop"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:shopRelationship attributes:@{ @"shopId": @"objectId" }];
[itemMapping addConnection:connection];

NS异常

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Item''

我错过了什么?

最佳答案

您需要向项目添加一个 transient 属性(称为 shopId)和关联的映射。

使用外键映射将关系配置为:

NSEntityDescription *itemEntity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
NSRelationshipDescription *shopRelationship = [itemEntity relationshipsByName][@"shop"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:shopRelationship attributes:@{ @"shopId": @"id" }];

然后使用 addConnection: 将其添加到您的映射中。

关于ios - RestKit 与主键的关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18934828/

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