From 633251ac1786426bfe4ea30d7bd28e7e1009054e Mon Sep 17 00:00:00 2001 From: NicoleYarroch Date: Tue, 14 Jul 2020 12:04:25 -0400 Subject: Only dispatch once when removing observers Signed-off-by: NicoleYarroch --- SmartDeviceLink/SDLSystemCapabilityManager.m | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/SmartDeviceLink/SDLSystemCapabilityManager.m b/SmartDeviceLink/SDLSystemCapabilityManager.m index 3f7c4f8a8..e5817b9d7 100644 --- a/SmartDeviceLink/SDLSystemCapabilityManager.m +++ b/SmartDeviceLink/SDLSystemCapabilityManager.m @@ -558,6 +558,7 @@ typedef NSString * SDLServiceID; /// Helper method for subscribing to a system capability type /// @param type The SystemCapabilityType that will be subscribed /// @param observerObject An object that can be used to unsubscribe the block. If nil, the subscription was not succesful. +/// @return The observer if the subscription was succesful; nil if not. - (nullable id)sdl_subscribeToCapabilityType:(SDLSystemCapabilityType)type observerObject:(SDLSystemCapabilityObserver *)observerObject { if ([self.currentHMILevel isEqualToEnum:SDLHMILevelNone] && ![type isEqualToEnum:SDLSystemCapabilityTypeDisplays]) { SDLLogE(@"Attempted to subscribe to type: %@ in HMI level NONE, which is not allowed. Please wait until you are in HMI BACKGROUND, LIMITED, or FULL before attempting to subscribe to any SystemCapabilityType other than DISPLAYS.", type); @@ -612,19 +613,17 @@ typedef NSString * SDLServiceID; // Loop through our observers for (SDLSystemCapabilityType key in self.capabilityObservers.allKeys) { for (SDLSystemCapabilityObserver *observer in self.capabilityObservers[key]) { - // If an observer object is nil, remove it - if (observer.observer == nil) { - [self sdl_runSyncOnQueue:^{ + [self sdl_runSyncOnQueue:^{ + // If an observer object is nil, remove it + if (observer.observer == nil) { [self.capabilityObservers[key] removeObject:observer]; - }]; - } + } - // If we no longer have any observers for that type, remove the array - if (self.capabilityObservers[key].count == 0) { - [self sdl_runSyncOnQueue:^{ + // If we no longer have any observers for that type, remove the array + if (self.capabilityObservers[key].count == 0) { [self.capabilityObservers removeObjectForKey:key]; - }]; - } + } + }]; } } -- cgit v1.2.1