OGeek|极客世界-中国程序员成长平台

标题: ios - NSManagedObject 作为 MKAnnotation 和核心数据并发 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:53
标题: ios - NSManagedObject 作为 MKAnnotation 和核心数据并发

我正在使用我认为是一个相当典型的 NSManagedObject 子类的实现,它符合 MKAnnotation 协议(protocol),以便在 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)setCoordinateCLLocationCoordinate2D)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 }]; block 中,但这会导致线程锁定失败。

我应该忽略错误并继续前进,还是为此目的有更好的做法?



Best Answer-推荐答案


我找不到原因。我验证了 NSManagedObject 在主队列上下文中。正在询问不是主队列的队列上的坐标。我所做的修复是使用代理对象作为传递给 MKMapview 的注释,而不是直接传递它。 NSObject 类符合 MKAnnotation 协议(protocol)。使用我的 NSManagedObject 中的坐标和标题进行初始化,传递它而不是真正的交易。

关于ios - NSManagedObject 作为 MKAnnotation 和核心数据并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075178/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4