【发布时间】:2015-03-20 10:36:56
【问题描述】:
我正在使用我认为是相当典型的NSManagedObject 子类实现,它符合MKAnnotation 协议,以便在MKMapView 中显示。查看 setter 和 getter:
-(CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D coord = EMPTY_LOCATION_COORDINATE;
BOOL validLong = (self.longitude != nil) && ([self.longitude doubleValue] != 0);
BOOL validLat = (self.latitude != nil) && ([self.latitude doubleValue] != 0);
if (validLong && validLat) {
coord.longitude = [self.longitude doubleValue];
coord.latitude = [self.latitude doubleValue];
}
return coord;
}
-(void)setCoordinate:(CLLocationCoordinate2D)coordinate {
if (coordinate.latitude != EMPTY_LOCATION && coordinate.longitude != EMPTY_LOCATION) {
self.latitude = [NSNumber numberWithDouble:coordinate.latitude];
self.longitude = [NSNumber numberWithDouble:coordinate.longitude];
} else {
self.latitude = nil;
self.longitude = nil;
}
}
-(NSString *)title {
NSString *str = [self.projectName copy];
return str;
}
这是有效的,根本不会在生产中引起问题。
我正在使用 Core Data 多线程断言调试一些 Core Data 并发问题,我发现它会将 gutter 标记为并发违规。我的猜测是调用坐标的MKMapview 正在使用后台线程,从技术上讲这是不允许的。可以想象,它不能保证它在生产中工作。
我尝试将 getter 包装在 [self.managedObjectContext performBlockAndWait^(void){ //set here }]; 块中,但这会导致线程锁定失败。
我应该忽略错误并继续前进,还是为此目的有更好的做法?
【问题讨论】:
-
您遇到的确切错误是什么?您可以在 getter 中放置一个断点来检查它被调用的线程。另外,您的托管对象所在的
NSManagedObjectContext是什么concurrencyType? -
我没有收到错误消息,它只是在给自己的第一条消息上中断。我相信 moc 的并发类型是主队列,但现在你提出来了,我需要检查一下。有可能我在某个时候改变了这一点而没有考虑这个问题。
标签: ios objective-c core-data thread-safety mkannotation