Skip to content

Commit

Permalink
Remove -[NSNotificationCenter removeObserver:] in deallocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmagman committed May 11, 2024
1 parent c9d07ce commit 02a177c
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ - (instancetype)initWithName:(NSString*)name project:(nullable FlutterDartProjec
}

- (void)dealloc {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self];
[_name release];
[_engines release];
[_project release];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ - (instancetype)init {
return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setMaxRefreshRate:(double)refreshRate forceMax:(BOOL)forceMax {
// This is copied from vsync_waiter_ios.mm. The vsync waiter has display link scheduled on UI
// thread which does not trigger actual core animation frame. As a workaround FlutterMetalLayer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ - (void)handleWillTerminate:(NSNotification*)notification
@end

@implementation FlutterPluginAppLifeCycleDelegate {
NSMutableArray* _notificationUnsubscribers;
UIBackgroundTaskIdentifier _debugBackgroundTask;

// Weak references to registered plugins.
Expand All @@ -40,16 +39,10 @@ @implementation FlutterPluginAppLifeCycleDelegate {

- (void)addObserverFor:(NSString*)name selector:(SEL)selector {
[[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil];
__block NSObject* blockSelf = self;
dispatch_block_t unsubscribe = ^{
[[NSNotificationCenter defaultCenter] removeObserver:blockSelf name:name object:nil];
};
[_notificationUnsubscribers addObject:[unsubscribe copy]];
}

- (instancetype)init {
if (self = [super init]) {
_notificationUnsubscribers = [[NSMutableArray alloc] init];
std::string cachePath = fml::paths::JoinPaths({getenv("HOME"), kCallbackCacheSubDir});
[FlutterCallbackCache setCachePath:[NSString stringWithUTF8String:cachePath.c_str()]];
#if not APPLICATION_EXTENSION_API_ONLY
Expand All @@ -70,12 +63,6 @@ - (instancetype)init {
return self;
}

- (void)dealloc {
for (dispatch_block_t unsubscribe in _notificationUnsubscribers) {
unsubscribe();
}
}

static BOOL IsPowerOfTwo(NSUInteger x) {
return x != 0 && (x & (x - 1)) == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,14 @@
#include "flutter/fml/logging.h"
#include "flutter/fml/paths.h"

@implementation FlutterAppLifecycleRegistrar {
NSMutableArray* _notificationUnsubscribers;
}
@implementation FlutterAppLifecycleRegistrar

- (void)addObserverFor:(NSString*)name selector:(SEL)selector {
[[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil];
__block NSObject* blockSelf = self;
dispatch_block_t unsubscribe = ^{
[[NSNotificationCenter defaultCenter] removeObserver:blockSelf name:name object:nil];
};
[_notificationUnsubscribers addObject:[unsubscribe copy]];
}

- (instancetype)init {
if (self = [super init]) {
_notificationUnsubscribers = [[NSMutableArray alloc] init];

// Using a macro to avoid errors where the notification doesn't match the
// selector.
#ifdef OBSERVE_NOTIFICATION
Expand Down Expand Up @@ -60,15 +51,6 @@ - (instancetype)init {
return self;
}

- (void)dealloc {
for (dispatch_block_t unsubscribe in _notificationUnsubscribers) {
unsubscribe();
}
[_notificationUnsubscribers removeAllObjects];
_delegates = nil;
_notificationUnsubscribers = nil;
}

static BOOL IsPowerOfTwo(NSUInteger x) {
return x != 0 && (x & (x - 1)) == 0;
}
Expand Down

0 comments on commit 02a177c

Please sign in to comment.