我試着在StackOverflow上查找所有這些問題,並找不到任何幫助我的東西。我甚至無法自己複製它,我通過iTunes Connect從實際用戶那裏得到了這一點。這是在Xcode中的崩潰日誌:NSArrayM被枚舉時發生了變異 - 添加對象,而不是刪除
這是我的全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'本身是可變的呢?而如果這個後臺線程持有對它的引用,它會被別的東西改變呢?我沒有看到你在這裏做任何事情來使這個代碼是線程安全的。 – matt
是的,它發生在後臺線程上。謹慎分享我可以如何使這段代碼線程安全? – Jan
我應該只是創建一個位置的副本,並枚舉該副本? – Jan