Skip to content

Commit

Permalink
Fixes #17
Browse files Browse the repository at this point in the history
Avoid retain cycles of caputred self inside blocks. Instead of
using __block typeof(self) this = self; before a block we use
__weak typeof(self) wself = self; outside and
__strong typeof(self) this = wself; inside the block.
  • Loading branch information
Guido Marucci Blas committed Jan 6, 2015
1 parent 652ce52 commit 61018cd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
12 changes: 8 additions & 4 deletions Pod/Classes/WLXBluetoothConnectionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ - (void)startConnectionTerminationTimerWithTimeout:(NSUInteger)timeout forPeriph
(unsigned long)timeout, (self.connected) ? @"YES" : @"NO", (self.connecting) ? @"YES" : @"NO",
(self.reconnecting) ? @"YES" : @"NO");

__block typeof(self) this = self;
__weak typeof(self) wself = self;
[self.connectionTimerExecutor after:timeout dispatchBlock:^{
__strong typeof(self) this = wself;
WLXLogDebug(@"Connection timer has expired. Connected '%@'. Connecting '%@'. Reconnecting '%@'.",
(self.connected) ? @"YES" : @"NO", (self.connecting) ? @"YES" : @"NO",
(self.reconnecting) ? @"YES" : @"NO");
Expand Down Expand Up @@ -261,8 +262,9 @@ - (BOOL)canConnectUsingBlock:(void(^)(NSError *))block {

- (void)tryToReconnect:(NSError *)error {
_reconnecting = YES;
__block typeof(self) this = self;
__weak typeof(self) wself = self;
BOOL willTryToReconnect = [self.reconnectionStrategy tryToReconnectUsingConnectionBlock:^{
__strong typeof(self) this = wself;
[this connectWithTimeout:this.reconnectionStrategy.connectionTimeout usingBlock:nil];
}];
if (!willTryToReconnect) {
Expand All @@ -275,26 +277,28 @@ - (void)tryToReconnect:(NSError *)error {
NSDictionary * userInfo = @{
WLXBluetoothDeviceRemainingReconnectionAttemps : @(remainingAttemps)
};
[self.notificationCenter postNotificationName:WLXBluetoothDeviceReconnecting object:this userInfo:userInfo];
[self.notificationCenter postNotificationName:WLXBluetoothDeviceReconnecting object:self userInfo:userInfo];
if ([self.delegate respondsToSelector:@selector(connectionManager:willAttemptToReconnect:)]) {
[self.delegate connectionManager:self willAttemptToReconnect:remainingAttemps];
}
}
}

- (void)registerNotificationHandlers {
__block typeof(self) this = self;
__weak typeof(self) wself = self;
self.handlers = @[
[self.notificationCenter addObserverForName:WLXBluetoothDeviceBluetoothIsOn
object:nil
queue:nil
usingBlock:^(NSNotification * notification){
__strong typeof(self) this = wself;
this.bluetoothOn = YES;
}],
[self.notificationCenter addObserverForName:WLXBluetoothDeviceBluetoothIsOff
object:nil
queue:nil
usingBlock:^(NSNotification * notification){
__strong typeof(self) this = wself;
this.bluetoothOn = NO;
}]
];
Expand Down
7 changes: 5 additions & 2 deletions Pod/Classes/WLXBluetoothDeviceDiscoverer.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ - (BOOL)deviceAlreadyDiscovered:(WLXDeviceDiscoveryData *)discoveryData {

- (void)startDiscoveryTerminationTimerWithTimeout:(NSUInteger)timeout {
WLXLogVerbose(@"Discovery timer started with timeout %lu", (unsigned long)timeout);
__block typeof(self) this = self;
__weak typeof(self) wself = self;
[self.discoveryTimerExecutor after:timeout dispatchBlock:^(){
__strong typeof(self) this = wself;
WLXLogDebug(@"Discovery timer has experied");
if (this.discovering) {
[this stopDiscoveringDevices];
Expand All @@ -164,18 +165,20 @@ - (NSRegularExpression *)buildRegularExpression:(NSString *)pattern {
}

- (void)registerNotificationHandlers {
__block typeof(self) this = self;
__weak typeof(self) wself = self;
self.handlers = @[
[self.notificationCenter addObserverForName:WLXBluetoothDeviceBluetoothIsOn
object:nil
queue:nil
usingBlock:^(NSNotification * notification) {
__strong typeof(self) this = wself;
this.bluetoothOn = YES;
}],
[self.notificationCenter addObserverForName:WLXBluetoothDeviceBluetoothIsOff
object:nil
queue:nil
usingBlock:^(NSNotification * notification) {
__strong typeof(self) this = wself;
this.bluetoothOn = NO;
[this stopDiscoveringDevices];
}]
Expand Down
3 changes: 2 additions & 1 deletion Pod/Classes/WLXBluetoothDeviceRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ - (void)saveLastConnectedPeripheral:(CBPeripheral *)peripheral {
}

- (void)registerNotificationHandlers {
__block typeof(self) this = self;
__weak typeof(self) wself = self;
self.notificationHandlers = @[
[self.notificationCenter addObserverForName:WLXBluetoothDeviceConnectionEstablished
object:nil
queue:nil
usingBlock:^(NSNotification * notification){
CBPeripheral * peripheral = notification.userInfo[WLXBluetoothDevicePeripheral];
__strong typeof(self) this = wself;
[this saveLastConnectedPeripheral:peripheral];
}]
];
Expand Down
12 changes: 8 additions & 4 deletions Pod/Classes/WLXServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ - (void)readValueFromCharacteristic:(CBUUID *)characteristicUUID usingBlock:(voi
WLXAssertNotNil(characteristicUUID);
WLXAssertNotNil(block);
WLXLogVerbose(@"Trying to read value for characteristic %@", characteristicUUID.UUIDString);
__block typeof(self) this = self;
__weak typeof(self) wself = self;
[self.asyncExecutor executeBlock:^(NSError * error, CBCharacteristic * characteristic) {
__strong typeof(self) this = wself;
if (error) {
DISPATCH(block(error, nil));
} else {
Expand All @@ -87,8 +88,9 @@ - (void)writeValue:(NSData *)data toCharacteristic:(CBUUID *)characteristicUUID
WLXAssertNotNil(block);
WLXAssertNotNil(data);
WLXLogVerbose(@"Trying to write value for characteristic %@", characteristicUUID.UUIDString);
__block typeof(self) this = self;
__weak typeof(self) wself = self;
[self.asyncExecutor executeBlock:^(NSError * error, CBCharacteristic * characteristic) {
__strong typeof(self) this = wself;
if (error) {
DISPATCH(block(error));
} else {
Expand All @@ -102,9 +104,10 @@ - (void)writeValue:(NSData *)data toCharacteristic:(CBUUID *)characteristicUUID
- (void)writeValue:(NSData *)data toCharacteristic:(CBUUID *)characteristicUUID {
WLXAssertNotNil(characteristicUUID);
WLXAssertNotNil(data);
__block typeof(self) this = self;
__weak typeof(self) wself = self;
WLXLogVerbose(@"Trying to write value for characteristic %@", characteristicUUID.UUIDString);
[self.asyncExecutor executeBlock:^(NSError * error, CBCharacteristic * characteristic) {
__strong typeof(self) this = wself;
if (!error) {
WLXLogDebug(@"Writting value for characteristic %@ without response", characteristicUUID);
[this.peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
Expand Down Expand Up @@ -248,9 +251,10 @@ - (void)didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characte
- (void)setNotification:(BOOL)enabled forCharacteristic:(CBUUID *)characteristicUUID usingBlock:(void(^)(NSError *))block {
WLXAssertNotNil(characteristicUUID);
WLXAssertNotNil(block);
__block typeof(self) this = self;
__weak typeof(self) wself = self;
WLXLogVerbose(@"Trying to set notification state for characteristic %@ to %@", characteristicUUID.UUIDString, (enabled) ? @"YES" : @"NO");
[self.asyncExecutor executeBlock:^(NSError * error, CBCharacteristic * characteristic) {
__strong typeof(self) this = wself;
if (error) {
DISPATCH(block(error));
} else {
Expand Down
7 changes: 5 additions & 2 deletions Pod/Classes/WLXServicesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ - (void)stopDiscoveringServices {
}

- (void)registerNotificationHandlers {
__block typeof(self) this = self;
id handler = ^(NSNotification * notification) { [this stopDiscoveringServices]; };
__weak typeof(self) wself = self;
id handler = ^(NSNotification * notification) {
__strong typeof(self) this = wself;
[this stopDiscoveringServices];
};
[self registerHandler:handler forNotifications:@[
WLXBluetoothDeviceBluetoothIsOff,
WLXBluetoothDeviceConnectionTerminated,
Expand Down

0 comments on commit 61018cd

Please sign in to comment.