在使用正确的值更新Core数据后,我想将这些值添加到地图视图中。 这是我为此而编写的代码:
-(void)updateMapWithPredicate:(NSPredicate *)predicate
{
int numberOfElementiOnMap = 0;
dispatch_async(dispatch_get_main_queue(), ^{
[self.ActiviIndicator startAnimating];
[self.mapView removeAnnotations:self.mapView.annotations];
});
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
if (predicate) {
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"(latitude != nil) AND (longitude != nil)"]
,predicate]];
} else {
request.predicate = [NSPredicate predicateWithFormat:@"(latitude != nil) AND (longitude != nil)"];
}
//Get all different coordinate from core data.
[request setPropertiesToFetch:@[@"latitude", @"longitude"]];
[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];
// Execute the fetch
NSError *error = nil;
NSArray *photos = [self.context executeFetchRequest:request error:&error];
for (NSDictionary *coordinateDic in photos) {
NSPredicate *predicateToSend;
if (predicate) {
/* Get all pictures that corresponding to the coordinate inside the coordinateDic
*/
predicateToSend = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)",
[coordinateDic valueForKey:@"latitude"], [coordinateDic valueForKey:@"longitude"]],predicate]];
} else {
predicateToSend = [NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)",[coordinateDic valueForKey:@"latitude"], [coordinateDic valueForKey:@"longitude"]];
}
dispatch_async(dispatch_get_main_queue(), ^{
//get all picture with the same lautide and longitude that are specified on predicate
NSArray *photos2 = [Photo pictureFromContext:self.context withPredicate:predicateToSend];
//if the pictures are more then one, we add only one picture, and we write the number of pictures as a subtitle of the mkannotationview
if ([photos2 count] > 1) {
Photo *photo = [photos2 lastObject];
photo.photoDescription = [NSString stringWithFormat:@"album with %lu picures",(unsigned long)[photos2 count]];
if (photo) {
// [self.mapView addAnnotation:photo];
}
} else { //add the only pictures
if ([photos2 firstObject]) {
[self.mapView addAnnotation:[photos2 firstObject]];
}
}
});
}
}
在这段代码中,我基本上从核心日期中检索需要的值,然后将其添加到地图中。
我添加到地图中的对象显然符合<MKAnnotation>
协议。我在[self.mapView addAnnotation:[photos2 firstObject]];
行上收到错误。奇怪的是,我从核心日期获得的某些对象被正确添加到地图中。我尝试通过启用NSZombie来调试代码,但我得到了同样的错误。我也检查坐标和它的一切。我在调试代码时发现的唯一区别是:
正确添加到地图的对象在调试器中如下所示:
相反,当我收到错误时,我添加到地图的对象在模拟器上看起来像这样:
当我打印的内容为:0我得到这个:
Printing description of *([0]->[0]):
(Photo_Photo_) [0] = {}
它看起来像是指向空实体的指针。 你知道我怎么解决这个问题吗? 感谢
答案 0 :(得分:0)
提交错误报告后,Apple告诉我解决问题的方法。问题是我正在以非KVO兼容的方式更新坐标。我存储像你这样的属性,纬度和经度。尝试在Photo类上添加它。
- (void)willChangeValueForKey:(NSString *)key
{
if ([key isEqualToString:@"latitude"] || [key isEqualToString:@"longitude"]) { [self willChangeValueForKey:@"coordinate"]; }
[super willChangeValueForKey:key];
}
- (void)didChangeValueForKey:(NSString *)key
{
if ([key isEqualToString:@"latitude"] || [key isEqualToString:@"longitude"]) { [self didChangeValueForKey:@"coordinate"]; }
[super didChangeValueForKey:key];
}
希望这有帮助。