创建NSManagedObject并在视图控制器之间传递它

时间:2012-08-18 01:06:14

标签: iphone core-data

所以我不确定这是怎么回事。我有一个名为Map的NSManagedObject子类。它是这样创建的:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
   // create map with below initializer
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapView.visibleMapRect);

   self.map = [Map mapWithName:nil: region:region inManagedObjectContext:self.managedObjectContext;
}

+ (Map *)mapWithName:(NSString *)mapName region:(MKCoordinateRegion)region inManagedObjectContext:(NSManagedObjectContext *)context {
    Map *aMap = [Map mapWithName:mapName inManagedObjectContext:context];
    aMap.centerLatitude = @(region.center.latitude);
    aMap.centerLongitude = @(region.center.longitude);
    aMap.latitudeDelta = @(region.span.latitudeDelta);
    aMap.longitudeDelta = @(region.span.longitudeDelta);

    return aMap;
}

我将Map对象放在这里,因为委托方法

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView

并不总是被调用,但是regionDidChangeAnimated:是的。问题是,这可以在NSManagedObjectContext中创建1-7个映射对象。因此,当我进入下一个屏幕时,即使我只有一个self.map属性,我的fetchedResultsController.fetchedObjects将包含1-7个地图对象,并在下一页上用一堆相同的对象填充TableViewController。有一个更好的方法吗?我以为我可以制作一个常规的MapTemp:NSObject模仿NSManagedObject,只在我需要保存Map或其他东西时才创建Map:NSManagedObject。

思考?谢谢!

1 个答案:

答案 0 :(得分:-1)

如果您不想要多个对象,那么为什么要创建它们
删除您不想要的,或者更好的更新现有的。

相关问题