我在使用0.20.3中的新@root @parent访问器时遇到RestKit映射问题.我不确定这是一个错误还是对如何正确使用框架的误解.
问题
@root和@parent的新概念似乎对我不起作用.
编辑:删除了一堆关于我认为问题是什么的讨论.我错了所以没有必要消化它.如果上面的问题陈述适用于你…那么这个SO帖子可能会帮助你走.
背景
示例源XML可以下载here
XML的基本结构如下:
<locations> <location lat="38.8561" lon="-94.6654" timezone="UTC" city="Overland Park" region="KS" country="US" zipcode="66223" offset="0" local_offset_hours="-5"> <sfc_ob> <attribute1></attribute1> </sfc_ob> <daily_summarIEs> <daily_summary> <attribute2> </attribute2> </daily_summary> </daily_summarIEs> <hourly_summarIEs> <hourly_summary> <attribute3></attribute3> </hourly_summary> </hourly_summarIEs> </location></locations>
我的核心数据实体如下:
RESTKIT相关代码
- (GLWeatherManager *)init {self = [super init];// setup loggingRKLogConfigureByname("RestKit/Network*",RKLogLevelTrace);RKLogConfigureByname("RestKit/ObjectMapPing",RKLogLevelTrace);self.httpClIEnt = [[AFhttpClIEnt alloc] initWithBaseURL:[NSURL URLWithString:@"http://weather.wdtinc.com"]];[self.httpClIEnt setDefaultheader:@"Accept" value:RKMIMETypeXML];[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];self.restKitManager = [[RKObjectManager alloc] initWithhttpClIEnt:self.httpClIEnt];self.restKitManager.managedobjectStore = [[RKManagedobjectStore alloc] initWithPersistentStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];[self.restKitManager.managedobjectStore createManagedobjectContexts];// LocationsRKEntityMapPing *locationMapPing = [self buildMapForLocations];RKEntityMapPing *currentConditionsMapPing = [self buildMapForCurrentConditions];RKEntityMapPing *dailySummarIEsMapPing = [self buildMapForDailySummarIEs];RKEntityMapPing *hourlyForecastsMapPing = [self buildMapForHourlyForecasts];[locationMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:@"daily_summarIEs" toKeyPath:@"dailySummarIEs" withMapPing:dailySummarIEsMapPing]];[locationMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:@"hourly_summarIEs" toKeyPath:@"hourlyForecasts" withMapPing:hourlyForecastsMapPing]];[locationMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:@"sfc_ob" toKeyPath:@"currentConditions" withMapPing:currentConditionsMapPing]];[dailySummarIEsMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:nil toKeyPath:@"location" withMapPing:locationMapPing]];[hourlyForecastsMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:nil toKeyPath:@"location" withMapPing:locationMapPing]];RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapPing:locationMapPing pathPattern:@"/Feeds/demoFeeds20131031/mega.PHP" keyPath:@"locations.location" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; // add mapPing description to objectmanager[self.restKitManager addResponseDescriptor:descriptor];RKResponseDescriptor *descriptor2 = [RKResponseDescriptor responseDescriptorWithMapPing:currentConditionsMapPing pathPattern:@"/Feeds/demoFeeds20131031/mega.PHP" keyPath:@"locations.location.sfc_ob" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];[self.restKitManager addResponseDescriptor:descriptor2];RKResponseDescriptor *descriptor3 = [RKResponseDescriptor responseDescriptorWithMapPing:dailySummarIEsMapPing pathPattern:@"/Feeds/demoFeeds20131031/mega.PHP" keyPath:@"locations.location.daily_summarIEs.daily_summary" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [self.restKitManager addResponseDescriptor:descriptor3]; RKResponseDescriptor *descriptor4 = [RKResponseDescriptor responseDescriptorWithMapPing:hourlyForecastsMapPing pathPattern:@"/Feeds/demoFeeds20131031/mega.PHP" keyPath:@"locations.location.hourly_summarIEs.hourly_summary" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [self.restKitManager addResponseDescriptor:descriptor4];// start the location manager to get the current locationself.locationManager = [[CLLocationManager alloc] init];[self.locationManager setDelegate:self];[self.locationManager startUpdatingLocation];self.locations = [NSMutableArray arrayWithArray:[Locations findAll]];[self getMegaFeed];return self;}- (RKEntityMapPing *)buildMapForLocations {RKEntityMapPing *locationMapPing = [RKEntityMapPing mapPingForEntityForname:@"Locations" inManagedobjectStore:self.restKitManager.managedobjectStore];[locationMapPing addAttributeMapPingsFromDictionary:@{ @"lat" : @"latitude",@"lon" : @"longitude",@"city" : @"city",@"region" : @"region",@"country" : @"country",@"zipcode" : @"zipcode",}];locationMapPing.IDentificationAttributes = [NSArray arrayWithObject:@"zipcode"];return locationMapPing;} - (RKEntityMapPing *)buildMapForCurrentConditions { // Current Conditions RKEntityMapPing *mapPing = [RKEntityMapPing mapPingForEntityForname:@"CurrentConditions" inManagedobjectStore:self.restKitManager.managedobjectStore]; [mapPing addAttributeMapPingsFromDictionary:@{ //@"stn" : @"stn",//@"location" : @"location",//@"stn_lat" : @"stnLatitude",//@"stn_lon" : @"stnLongitude",@"ob_time.text" : @"observationTime",@"day_of_week.text" : @"dayOfWeek",@"temp_C.text" : @"temperatureMetric",@"temp_F.text" : @"temperatureImperial",@"dewp_C.text" : @"dewPointMetric",@"dewp_F.text" : @"dewPointImperial",@"rh_pct.text" : @"relativeHumIDity",@"wnd_dir.text" : @"windDirection",@"wnd_spd_mph.text" : @"windSpeedImperial",@"wnd_spd_kph.text" : @"windSpeedMetric",@"press_in.text" : @"pressureImperial",@"press_mb.text" : @"pressureMetric",@"wx.text" : @"conditionSummary",@"wx_code.text" : @"conditionCode",@"cld_cover.text" : @"cloudCover",@"visibility_ft.text" : @"visibilityImperial",@"visibility_m.text" : @"visibilityMetric",@"apparent_temp_F.text" : @"feelslikeTemperatureImperial",@"apparent_temp_C.text" : @"feelslikeTemperatureMetric",@"moon_phase.text" : @"moonPhase",@"sunrise_utc.text" : @"sunrise",@"sunset_utc.text" : @"sunset" }]; [mapPing setIDentificationAttributes:[NSArray arrayWithObjects:@"observationTime",nil]]; return mapPing;}- (RKEntityMapPing *)buildMapForDailySummarIEs {RKEntityMapPing *mapPing = [RKEntityMapPing mapPingForEntityForname:@"DailySummarIEs" inManagedobjectStore:self.restKitManager.managedobjectStore];[mapPing addAttributeMapPingsFromDictionary:@{ @"summary_date.text" : @"date",@"max_temp_F.text" : @"tempMaxImperial",@"max_temp_C.text" : @"tempMaxMetric",@"min_temp_F.text" : @"tempMinImperial",@"min_temp_C.text" : @"tempMinMetric",@"min_wnd_spd_mph.text" : @"windSpeedMinImperial",@"min_wnd_spd_kph.text" : @"windSpeedMinMetric",@"max_wnd_spd_mph.text" : @"windSpeedMaxImperial",@"max_wnd_spd_kph.text" : @"windSpeedMaxMetric",@"wnd_gust_mph.text" : @"windGustImperial",@"wnd_gust_kph.text" : @"windGustMetric",@"pop.text" : @"probabilityOfPrecipitation",@"text_description.text" : @"textDescription",@"sunset_utc.text" : @"sunset",@"@root.locations.location.zipcode" : @"locationZipcode" }];mapPing.IDentificationAttributes = [NSArray arrayWithObjects:@"date",@"locationZipcode",nil];[mapPing addConnectionForRelationship:@"location" connectedBy:@{@"locationZipcode": @"zipcode"}];return mapPing;
}
- (RKEntityMapPing *)buildMapForHourlyForecasts {RKEntityMapPing *mapPing = [RKEntityMapPing mapPingForEntityForname:@"HourlyForecasts" inManagedobjectStore:self.restKitManager.managedobjectStore];[mapPing addAttributeMapPingsFromDictionary:@{ @"day_of_week_utc.text" : @"dayOfWeek",@"time_utc.text" : @"forecastTime",@"app_temp_C.text" : @"feelslikeTemperatureMetric",@"app_temp_F.text" : @"feelslikeTemperatureImperial",@"day_night.text" : @"dayNight",@"sky_cov_pct.text" : @"skyCoverPercent",@"wnd_dir_degs.text" : @"windDirectiondegrees",@"@root.locations.location.zipcode" : @"locationZipcode" }];mapPing.IDentificationAttributes = [NSArray arrayWithObjects:@"forecastTime",nil];[mapPing addConnectionForRelationship:@"location" connectedBy:@{@"locationZipcode": @"zipcode"}];return mapPing;
}
- (voID)getMegaFeed {for (Locations *location in self.locations) { Nsstring *path = [Nsstring stringWithFormat:@"/Feeds/demoFeeds20131031/mega.PHP?ZIP=%@&UNITS=all",location.zipcode]; // fetch data [self.restKitManager getobjectsAtPath:path parameters:nil success:^(RKObjectRequestoperation *operation,RKMapPingResult *mapPingResult) { NSArray *mappedobjects = [mapPingResult array]; NSMutableArray *validobjectIDs = [[NSMutableArray alloc] initWithCapacity:[mappedobjects count]]; for (NSManagedobject *object in mappedobjects) { NSManagedobjectID *moID = [object objectID]; [validobjectIDs addobject:moID]; } [self notifyObservers:@selector(megaFeedDIDFinish:location:) withObject:validobjectIDs withObject:location]; } failure:^(RKObjectRequestoperation *operation,NSError *error) { [REUtility showDefaultAlertWithError:error]; [RELog error:@"%s Hit error:%@",__func__,error]; }];}
}
解决方法 我认为问题是您在尝试使用@root的映射的响应描述符中使用的关键路径.当您在响应描述符上指定键路径时,您实际上正在更改@root对象,因为您正在深入到指定深度的内容,并且该深度成为该映射的根.如果在映射过程中进行调试并查看提供的元数据,则应该能够查看/验证这一点.我不清楚为什么你有这么多不同的响应描述符.对于映射定义所有不同部分(及其映射)之间的所有关系的位置,有一个响应描述符似乎更合乎逻辑.通过这种方式工作,您可以获得更简单的代码,并且您还可以访问@root / @parent.
总结以上是内存溢出为你收集整理的ios – 用于映射的RestKit @root @parent访问器全部内容,希望文章能够帮你解决ios – 用于映射的RestKit @root @parent访问器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)