gpt4 book ai didi

ios - 如何解决 dispatch_group_leave 崩溃

转载 作者:行者123 更新时间:2023-11-28 23:20:21 35 4
gpt4 key购买 nike

我知道 dispatch_group_leave 调用需要与 dispatch_group_enter 平衡。这是发生崩溃的代码块(随机发生,并非总是如此)

            dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self getVibrationHistorySuccess:^(NSArray<VibrationPointDTO *> *vibrationPoints) {
self.vibrationPoints = vibrationPoints;
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
}];

dispatch_group_enter(group);
[self getTemperatureHistorySuccess:^(NSArray<TemperatureSetDTO *> *sets) {
self.temperatureSets = sets;
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
}];

dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
[self showLoading:NO];
[self updateChartWithVibrationHistory:self.vibrationPoints temperatureSets:self.temperatureSets];
});

有 2 次进入和 2 次退出(在每个闭包上,只有成功或失败的可能,所以只有这 2 次发生之一)

那么为什么我的代码会这样崩溃呢?我该如何解决这个问题?

enter image description here

想到的唯一另一件事是,该对象的多个实例确实会同时使用。这些不同的实例是否可能正在使用同一个组?是否有办法确保组本身在这些实例之间是唯一的?否则我想不出为什么会这样。

PS:我还添加了 swift 标签,以便更多地关注这个问题,因为无论使用何种语言,同样的问题都可能发生

编辑 1

根据收到的评论添加缺失的方法:

这是崩溃的 getTemperatureHistorySuccess

-(void)getTemperatureHistorySuccess:(void (^)(NSArray<TemperatureSetDTO*> *sets))success
failure:(void(^)(NSError *error))failure{
NSMutableArray *sets = [NSMutableArray new];
dispatch_group_t group = dispatch_group_create();

for (TemperatureSummaryNew *sensor in self.regionPCA.temperatureSensors) {
dispatch_group_enter(group);
[TemperatureSensorsService getHistory:sensor.temperatureSensorID startDate:self.startDate stopDate:self.stopDate interval:self.currentInterval success:^(NSArray<TemperaturePointDTO *> *result) {
TemperatureSetDTO *set = [TemperatureSetDTO new];
set.points = result;
[sets addObject:set];
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
}];
}

dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
success(sets);
});
}

这是在循环中调用的 getHistory:

+ (void) getHistory:(int)sensorID
startDate:(NSDate*)startDate
stopDate:(NSDate*)stopDate
interval:(HistoryInterval)interval
success:(void (^)(NSArray<TemperaturePointDTO*>* result))success
failure:(void(^)(NSError *error))failure{
ApiManager *api = [ApiManager sharedManager];

NSDictionary *params = @{
@"TemperatureSensorID" : [Converter toDictionaryValueFromInt:sensorID],
@"StartDate" : [Converter toDictionaryValueGMTFromDate:startDate],
@"StopDate" : [Converter toDictionaryValueGMTFromDate:stopDate],
@"Interval" : [Converter toDictionaryValueFromHistoryInterval:interval]};
[api POST:kApiTemperatureSensorsGetHistory parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) {
NSArray* res = [Converter fromDictionaryValueToTemperaturePointDTOArray:responseObject];
success(res);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}

这是getVibrationHistorySuccess

-(void)getVibrationHistorySuccess:(void (^)(NSArray<VibrationPointDTO *> *vibrationPoints))success
failure:(void(^)(NSError *error))failure{
__block NSArray<VibrationPointDTO *> *points;
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self getDistanceHistorySuccess:^{
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[VibrationSensorsService getHistory:self.regionPCA.regionID startDate:self.startDate stopDate:self.stopDate interval:self.currentInterval success:^(NSArray<VibrationPointDTO *> *result) {
points = result;
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
failure(error);
}];
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
success(points);
});
}

VibrationSensorsService getHistory 与上一个完全相同,但指向不同的端点

最佳答案

这是从您的问题中复制的 getVibrationHistorySuccess:failure: 的最后一部分:

[VibrationSensorsService getHistory:self.regionPCA.regionID startDate:self.startDate stopDate:self.stopDate interval:self.currentInterval success:^(NSArray<VibrationPointDTO *> *result) {
points = result;
dispatch_group_leave(group);
} failure:^(NSError *error) {
dispatch_group_leave(group);
failure(error);
}];
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
success(points);
});

这段代码总是调用success(points)。此外,它有时 调用failure(error)。这意味着可以执行 successfailure block 。

关于ios - 如何解决 dispatch_group_leave 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59721196/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com