iOS: remove sync in dealloc & add some weak-strong-dance

This commit is contained in:
王劲鹏 2021-08-19 11:43:49 +08:00 committed by osborn
parent 67915e6db0
commit e0d76d218d

View File

@ -87,24 +87,17 @@ - (void)subscribe:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
} }
- (void)unsubscribe:(NSString *)subscribeId withPromise:(DoricPromise *)promise { - (void)unsubscribe:(NSString *)subscribeId withPromise:(DoricPromise *)promise {
__block id observer = nil; dispatch_barrier_async(self.syncQuene, ^{
dispatch_sync(self.syncQuene, ^{ id observer = [self.observers objectForKey:subscribeId];
observer = [self.observers objectForKey:subscribeId];
});
if (observer) {
[[NSNotificationCenter defaultCenter] removeObserver:observer]; [[NSNotificationCenter defaultCenter] removeObserver:observer];
dispatch_barrier_async(self.syncQuene, ^{ [self.observers removeObjectForKey:subscribeId];
[self.observers removeObjectForKey:subscribeId];
}); [promise resolve:nil];
} });
[promise resolve:nil];
} }
- (void)dealloc { - (void)dealloc {
__block NSArray *values; NSArray *values = [self.observers allValues];
dispatch_sync(self.syncQuene, ^{
values = [self.observers allValues];
});
[values enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { [values enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
[[NSNotificationCenter defaultCenter] removeObserver:obj]; [[NSNotificationCenter defaultCenter] removeObserver:obj];
}]; }];