来自CoreData的获取实体不会在MKMapVIew上进行注释

时间:2013-09-25 16:11:01

标签: ios core-data annotations mkmapview mkannotation

当我尝试在当前的MKMapView中添加注释时遇到问题。下面是我如何尝试获取存储在Core数据中的实体并将它们注释到当前显示的MKMapView中。

我面临的问题是,只有新添加的实体(从另一个视图控制器添加)才会在地图视图上进行注释。较旧的数据(如几周前的数据)会被提取并添加到数组中,但根本没有注释到地图视图中。

此外,即使数据是新添加的,当我关闭应用程序然后再次重新打开应用程序时,前一个会话中的数据也不会被注释。

'Whale'实体已经符合MKAnnotation协议。

我真的很困惑,并且不知道问题的原因。

- (void)getWhales:(NSInteger)days{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Whale" inManagedObjectContext:self.managedObjectContext]];

    NSError *error = nil;

    NSDateComponents *dateComponent = [[NSDateComponents alloc] init];
    [dateComponent setDay:days];
    NSDate *designatedDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponent toDate:_today options:0];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date > %@", designatedDate];
    NSLog(@"date is %@", designatedDate);
    [request setPredicate:predicate];

    _whales = [self.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"number of whales found: %d", [_whales count]);

    [self.map addAnnotations:_whales];
}

0 个答案:

没有答案