如何在iOS中将RestKit值存储到CoreData?
我已经创建了实体如何在iOS中将RestKit值存储到CoreData?,ios,core-data,mapping,restkit,nsmanagedobjectcontext,Ios,Core Data,Mapping,Restkit,Nsmanagedobjectcontext,我已经创建了实体eyu区域并添加了属性。为了解析RestKit值并将数据存储在CoreData中,我创建了两种方法 (void)createRegionMappingWithStore:(RKManagedObjectStore*)managedObjectStore saveInDelegate:(AppDelegate*)AppDelegate (NSArray*)getRegionData 我只是从我的NewConnectionViewController.m调用这个方法。我的问题是,我无
eyu区域
并添加了属性。为了解析RestKit
值并将数据存储在CoreData
中,我创建了两种方法
(void)createRegionMappingWithStore:(RKManagedObjectStore*)managedObjectStore saveInDelegate:(AppDelegate*)AppDelegate
(NSArray*)getRegionData
我只是从我的NewConnectionViewController.m
调用这个方法。我的问题是,我无法获取数据。有什么问题?这里我展示了我的代码:
DataAccessHandler.m
@implementation DataAccessHandler
+(void)createRegionMappingWithStore:(RKManagedObjectStore *)managedObjectStore saveInDelegate:(AppDelegate *)appDelegate{
RKEntityMapping *regionMapping = [RKEntityMapping mappingForEntityForName:@"EY_Region" inManagedObjectStore:managedObjectStore];
regionMapping.identificationAttributes = @[@"regionId"];
[regionMapping addAttributeMappingsFromDictionary:@{ @"id" : @"regionId", @"name" : @"regionName", @"code" : @"regionCode",@"last_update":@"lastUpdatedDate",@"is_delete":@"isDelete"}];
RKResponseDescriptor *regionResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:regionMapping
method:RKRequestMethodGET
pathPattern:EY_REGION
keyPath:@"data.data"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
];
[appDelegate createObjectManagerForurl:EY_BASE_URL andAddResponseDescriptor:regionResponseDescriptor];
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
}
+(NSArray *)getRegionData{
NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"EY_Region"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"regionId" ascending:YES];
fetchRequest.sortDescriptors = @[descriptor];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"fetched objects is: %@",fetchedObjects);
NSLog(@"size is: %lu",[fetchedObjects count]);
return fetchedObjects;
}
NewConnectionViewController.m
#import "NewConnectionViewController.h"
@implementation NewConnectionViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[DataAccessHandler createConnectionMappingWithStore:[self managedObjectStore] saveInDelegate:[self appDelegate]];
[self requestConnectionData];
}
我得到的错误是
NSLocalizedFailureReason=映射操作无法在搜索的关键路径处找到任何嵌套对象表示:
发现输入到映射器的表示在以下关键路径中包含嵌套对象表示:地址、圆、连接编号、分布、负载、计量编号、名称、阶段、区域、节、服务编号、服务状态、使用情况
这可能表明您错误配置了映射的关键路径。NSLocalizedDescription=在搜索的关键路径中未找到可映射的对象表示形式。keyPath=null}}首先,您的请求(未显示代码)是异步的,因此在查询核心数据时不会完成。考虑使用获取的结果控制器而不是数组,这样在添加新数据时得到更新。
其次,您的请求URL(EY_BASE_URL
)和路径模式(EY_REGION
)(您都没有给出详细信息)需要匹配,以便路径模式是请求URL中不包含在对象管理器的基本URL中的部分
第三,错误显示为keyPath=null
,但响应描述符代码显示为keyPath:@“data.data”
,因此显示的代码与实际运行的代码之间存在一些不匹配
第四个数据。数据
,甚至数据
都未列在错误消息的关键路径中。您需要根据响应中预期的内容验证密钥路径,并在密钥路径中仅包含需要钻取以查找要映射的真实数据的密钥
因此,您需要检查一些潜在的错误。谢谢。现在Fethched值是HTML格式的。我认为问题可能出在URL中。。。