Restkit 将数组映射到核心数据
Restkit mapping arrays to core data
我有以下 JSON:
{
"id": 1,
"editable": true,
"number": "1",
"name": "Text here",
"banner": "Text here",
"address": "Text here",
"city": "Text here",
"contact": "Text here",
"latitude": "Text here",
"longitude": "Text here",
"banner_id": 5,
"hours": [
{
"day": "monday",
"start": "8:00am",
"end": "5:00pm"
},
{
"day": "tuesday",
"start": "",
"end": ""
},
{
"day": "wednesday",
"start": "",
"end": ""
},
{
"day": "thursday",
"start": "",
"end": ""
},
{
"day": "friday",
"start": "",
"end": ""
},
{
"day": "saturday",
"start": "",
"end": ""
},
{
"day": "sunday",
"start": "",
"end": ""
}
]
}
我正在尝试创建包含小时数组的此对象的映射,我的代码如下:
RKObjectMapping* hoursMapping = [RKObjectMapping mappingForClass:[Hours class] ];
[hoursMapping addAttributeMappingsFromArray:@[ @"day", @"start", @"end"]];
RKObjectMapping* storeMapping = [RKObjectMapping mappingForClass:[Store class] ];
[storeMapping addAttributeMappingsFromArray:@[ @"id", @"editable", @"number", @"name", @"banner", @"address", @"city", @"contact", @"latitude", @"longitude", @"banner_id"]];
[storeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hours"
toKeyPath:@"hours"
withMapping:hoursMapping]];
RKResponseDescriptor *storeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping
method:RKRequestMethodAny
pathPattern:@"api/v2/stores.json"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
映射失败,返回的唯一错误是:
"The operation couldn't be completed"
我在这里做错了什么?我怎样才能让它映射成功?
Store.h
@interface Store : NSManagedObject
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSNumber * editable;
@property (nonatomic, retain) NSString * number;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * banner;
@property (nonatomic, retain) NSNumber * address;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * contact;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSNumber * banner_id;
@property (nonatomic, retain) NSSet *hours;
@end
@interface Store (CoreDataGeneratedAccessors)
- (void)addHoursObject:(Hours *)value;
- (void)removeHoursObject:(Hours *)value;
- (void)addHours:(NSSet *)values;
- (void)removeHours:(NSSet *)values;
@end
Hours.h
@interface Hours : NSManagedObject
@property (nonatomic, retain) NSString * day;
@property (nonatomic, retain) NSString * start;
@property (nonatomic, retain) NSString * end;
@end
对于任何试图找到此问题的解决方案的人:
问题是我正在使用带有核心数据的对象映射。
我将所有对对象映射的引用更改为实体映射。关系映射是相同的,但现在指向核心数据实体而不是 class。现在一切正常。
RKEntityMapping *storeMapping = [RKEntityMapping mappingForEntityForName:@"Store" inManagedObjectStore:managedObjectStore];
storeMapping.identificationAttributes = @[@"id"];
[storeMapping addAttributeMappingsFromArray:@[ @"id", @"editable", @"number", @"name", @"banner", @"address", @"city", @"contact", @"latitude", @"longitude", @"banner_id"]];
RKEntityMapping *hoursMapping = [RKEntityMapping mappingForEntityForName:@"Hours" inManagedObjectStore:managedObjectStore];
[hoursMapping addAttributeMappingsFromArray:@[@"day", @"start", @"end"]];
[storeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hours" toKeyPath:@"hours" withMapping:hoursMapping]];
RKResponseDescriptor *storeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping
method:RKRequestMethodAny
pathPattern:@"/api/v2/stores.json"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:storeResponseDescriptor];
我有以下 JSON:
{
"id": 1,
"editable": true,
"number": "1",
"name": "Text here",
"banner": "Text here",
"address": "Text here",
"city": "Text here",
"contact": "Text here",
"latitude": "Text here",
"longitude": "Text here",
"banner_id": 5,
"hours": [
{
"day": "monday",
"start": "8:00am",
"end": "5:00pm"
},
{
"day": "tuesday",
"start": "",
"end": ""
},
{
"day": "wednesday",
"start": "",
"end": ""
},
{
"day": "thursday",
"start": "",
"end": ""
},
{
"day": "friday",
"start": "",
"end": ""
},
{
"day": "saturday",
"start": "",
"end": ""
},
{
"day": "sunday",
"start": "",
"end": ""
}
]
}
我正在尝试创建包含小时数组的此对象的映射,我的代码如下:
RKObjectMapping* hoursMapping = [RKObjectMapping mappingForClass:[Hours class] ];
[hoursMapping addAttributeMappingsFromArray:@[ @"day", @"start", @"end"]];
RKObjectMapping* storeMapping = [RKObjectMapping mappingForClass:[Store class] ];
[storeMapping addAttributeMappingsFromArray:@[ @"id", @"editable", @"number", @"name", @"banner", @"address", @"city", @"contact", @"latitude", @"longitude", @"banner_id"]];
[storeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hours"
toKeyPath:@"hours"
withMapping:hoursMapping]];
RKResponseDescriptor *storeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping
method:RKRequestMethodAny
pathPattern:@"api/v2/stores.json"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
映射失败,返回的唯一错误是: "The operation couldn't be completed"
我在这里做错了什么?我怎样才能让它映射成功?
Store.h
@interface Store : NSManagedObject
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSNumber * editable;
@property (nonatomic, retain) NSString * number;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * banner;
@property (nonatomic, retain) NSNumber * address;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * contact;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, retain) NSString * longitude;
@property (nonatomic, retain) NSNumber * banner_id;
@property (nonatomic, retain) NSSet *hours;
@end
@interface Store (CoreDataGeneratedAccessors)
- (void)addHoursObject:(Hours *)value;
- (void)removeHoursObject:(Hours *)value;
- (void)addHours:(NSSet *)values;
- (void)removeHours:(NSSet *)values;
@end
Hours.h
@interface Hours : NSManagedObject
@property (nonatomic, retain) NSString * day;
@property (nonatomic, retain) NSString * start;
@property (nonatomic, retain) NSString * end;
@end
对于任何试图找到此问题的解决方案的人:
问题是我正在使用带有核心数据的对象映射。 我将所有对对象映射的引用更改为实体映射。关系映射是相同的,但现在指向核心数据实体而不是 class。现在一切正常。
RKEntityMapping *storeMapping = [RKEntityMapping mappingForEntityForName:@"Store" inManagedObjectStore:managedObjectStore];
storeMapping.identificationAttributes = @[@"id"];
[storeMapping addAttributeMappingsFromArray:@[ @"id", @"editable", @"number", @"name", @"banner", @"address", @"city", @"contact", @"latitude", @"longitude", @"banner_id"]];
RKEntityMapping *hoursMapping = [RKEntityMapping mappingForEntityForName:@"Hours" inManagedObjectStore:managedObjectStore];
[hoursMapping addAttributeMappingsFromArray:@[@"day", @"start", @"end"]];
[storeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hours" toKeyPath:@"hours" withMapping:hoursMapping]];
RKResponseDescriptor *storeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping
method:RKRequestMethodAny
pathPattern:@"/api/v2/stores.json"
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:storeResponseDescriptor];