I have a solution to the notification problem that works well, but I'm afraid it will be a bad idea.
I have a notification that should be handled by each instance of the class and the class itself. To handle this, I am registering a notification for the class and class instances. Since this is the same notification, I called the class and instance method the same. This follows the standard we set for what the notification handlers are called.
It is a bad idea? There are some hidden got'ca that I miss. Will I be confused with future developers?
+ (void)initialize { if (self == [SICOHTTPClient class]) { [[self notificationCenter] addObserver:self selector:@selector(authorizationDidChangeNotification:) name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } } - (id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (self) { self.parameterEncoding = AFJSONParameterEncoding; [self registerHTTPOperationClass:[SICOJSONRequestOperation class]]; [self setDefaultHeader:@"Accept" value:@"application/json"]; if ([[self class] defaultAuthorization]) [self setDefaultHeader:@"Authorization" value:[[self class] defaultAuthorization]]; [[[self class] notificationCenter] addObserver:self selector:@selector(authorizationDidChangeNotification:) name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } return self; } - (void)dealloc { [[[self class] notificationCenter] removeObserver:self name:SICOJSONRequestOperationAuthorizationDidChangeNotification object:nil]; } #pragma mark Notifications - (void)authorizationDidChangeNotification:(NSNotification *)notification { NSString *authorization = notification.userInfo[SICOJSONRequestOperationAuthorizationKey]; if ([authorization isKindOfClass:[NSString class]]) { [self setDefaultHeader:@"Authorization" value:authorization]; } else { [self clearAuthorizationHeader]; } } + (void)authorizationDidChangeNotification:(NSNotification *)notification { NSString *authorization = notification.userInfo[SICOJSONRequestOperationAuthorizationKey]; if ([authorization isKindOfClass:[NSString class]]) { [self setDefaultAuthorization:authorization]; } else { [self setDefaultAuthorization:nil]; } }
Jeffery thomas
source share