Yes, you have a save cycle, at least until a notification appears. When you access clients ivar in a block, the block will save itself. It will be saved by the block in the notification center until a notification occurs (since you delete the observer at the end of the block). If this is undesirable in your case, you can use a weak link to yourself.
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter]; __weak id weakSelf = self; id observer = [notifyCenter addObserverForName:queryHash object:nil queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification* notification) { if (weakSelf) { if ([[notification.userInfo objectForKey:kdatasetReturnKey] objectAtIndex:0]) { NSArray *rows = [[notification.userInfo objectForKey:kdatasetReturnKey] objectAtIndex:0]; if (weakSelf.clients) { weakSelf.clients = nil; } weakSelf.clients = [[NSMutableArray alloc] initWithCapacity:rows.count]; for (NSDictionary *row in rows) { [weakSelf.clients addObject:row]; } } else { NSLog(@"CLIENTS ERROR Returned: %@",[notification.userInfo objectForKey:kerrorReturnKey]); } } [[NSNotificationCenter defaultCenter] removeObserver:observer]; }];
I see no reason why you need a __block qualify observer .
It is also not clear that you are getting something from using a block-based API. If you don't want to worry about a potential hold cycle, you can simply use addObserver:selector:name:object: and put your recall body in an instance method.
Christopher pickslay
source share