我尝试在 StackOverflow 上查看所有这些问题,但找不到任何对我有帮助的东西。我什至不能自己复制它,我是通过 iTunes Connect 从实际用户那里得到的。这是 Xcode 上的崩溃日志:
这是我崩溃的完整 serializedLocations 方法,没有从中删除任何内容:
- (NSMutableArray *)serializedLocations:(NSArray *)locations withTimestamp:(NSInteger)timestamp{
NSMutableArray *serializedLocations = [NSMutableArray new];
if(locations){
for (CLLocation *location in locations) {
NSInteger locationTimeInterval = floor([location.timestamp timeIntervalSince1970] * 1000);
NSInteger t = locationTimeInterval - timestamp;
NSMutableDictionary *serializedLocation = [NSMutableDictionary new];
serializedLocation[@"x"] = [NSNumber numberWithDouble:location.coordinate.latitude];
serializedLocation[@"y"] = [NSNumber numberWithDouble:location.coordinate.longitude];
serializedLocation[@"a"] = [NSNumber numberWithDouble:location.horizontalAccuracy];
serializedLocation[@"v"] = [NSNumber numberWithDouble:location.speed];
serializedLocation[@"o"] = [NSNumber numberWithDouble:location.course];
serializedLocation[@"t"] = [NSNumber numberWithLong:t];
[serializedLocations addObject:serializedLocation];
}
}
return serializedLocations;
}
我似乎找不到缺陷。
- 我正在创建一个临时新数组。
- 我不更改正在枚举的数组。
- 我正在向临时数组添加新对象。
- 我要返回那个新的临时数组
编辑:
让所有其他方法运行的父级调用如下:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[ApiClient insertEvent:event withLocations:locations];
});
最佳答案
我猜测 locations
,虽然在这里输入为 NSArray,但实际上是一个 NSMutableArray — 而 this 是在您更改时正在改变的数组枚举它。如果您要在后台线程上运行此代码,则需要确保 locations
不会“在您的背后”发生突变。一种简单的方法是在调用站点制作 locations
的深拷贝并传递 that 而不是可变数组。 (请注意,在 serializedLocations:withTimestamp:
中进行深层复制是不够的,因为 locations
可能会在复制过程中发生变化。)
关于ios - 枚举时 NSArrayM 发生了变异 - 添加对象,而不是删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43193297/