I just finished a model class that represents an Instagram Networking client for my iOS app.
I am specifically looking for a review of my use of static constants. These are strings that represent notification names and notification userInfo
dictionary keys.
Here is my networking class:
POPInstagramNetworkingClient.h:
#import "AFHTTPSessionManager.h"
@interface POPInstagramNetworkingClient : AFHTTPSessionManager
+ (id)sharedPOPInstagramNetworkingClient;
- (instancetype)initWithBaseURL:(NSURL *)url;
- (void)requestPopularMedia;
- (void)requestMediaWithTag:(NSString *)tag;
@end
POPInstagramNetworkingClient.m:
#import "POPInstagramNetworkingClient.h"
#import <AFNetworking.h>
@implementation POPInstagramNetworkingClient
#pragma mark - Singleton
+ (instancetype)sharedPOPInstagramNetworkingClient
{
static POPInstagramNetworkingClient *sharedPOPInstagramNetworkingClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//Define base URL string
static NSString * const BaseURLString = @"https://api.instagram.com/v1/";
//Create our shared networking client for Instagram with base URL
sharedPOPInstagramNetworkingClient = [[POPInstagramNetworkingClient alloc]initWithBaseURL:[NSURL URLWithString:BaseURLString]];
});
return sharedPOPInstagramNetworkingClient;
}
#pragma mark - Initializer Methods
- (instancetype)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (self) {
//Set the serializers
self.requestSerializer = [AFJSONRequestSerializer serializer];
self.responseSerializer = [AFJSONResponseSerializer serializer];
}
return self;
}
#pragma mark - Instance Methods
- (void)requestPopularMedia
{
//Define notification names
static NSString * const kRequestForPopularMediaSuccessful = @"RequestForPopularMediaSuccessful";
static NSString * const kRequestForPopularMediaUnsuccessful = @"RequestForPopularMediaUnsuccessful";
//Create manager and execute GET method to retreive popular media
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"%@media/popular?client_id=76566d0e6d5a41069ea5e8c86fbbd509", self.baseURL] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//Post success notification
[[NSNotificationCenter defaultCenter]postNotificationName:kRequestForPopularMediaSuccessful object:nil userInfo:@{@"requestForPopularMediaResults": responseObject}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
//Post failure notificaton
[[NSNotificationCenter defaultCenter]postNotificationName:kRequestForPopularMediaUnsuccessful object:nil userInfo:@{@"requestForPopularMediaResults": error}];
}];
}
- (void)requestMediaWithTag:(NSString *)tag
{
NSLog(@"requesting media with tag");
//Define notification names
static NSString * const kRequestForMediaWithTagSuccessful = @"RequestForMediaWithTagSuccessful";
static NSString * const kRequestForMediaWithTagUnsuccessful = @"RequestForMediaWithTagUnsuccessful";
//new - @"%@tags/%@/media/recent?client_id=76566d0e6d5a41069ea5e8c86fbbd509"
//old - @"%@tags/search?q=%@&client_id=76566d0e6d5a41069ea5e8c86fbbd509"
//Create manager and execute GET method to retreive media with tag
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"%@tags/%@/media/recent?client_id=76566d0e6d5a41069ea5e8c86fbbd509", self.baseURL, tag] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success!");
NSLog(@"Response Object: %@", responseObject);
//Post success notification
[[NSNotificationCenter defaultCenter]postNotificationName:kRequestForMediaWithTagSuccessful object:nil userInfo:@{@"requestForTaggedMediaResults": responseObject}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
//Post failure notification
[[NSNotificationCenter defaultCenter]postNotificationName:kRequestForMediaWithTagUnsuccessful object:nil userInfo:@{@"requestForTaggedMediaResults": error}];
}];
}
@end
I was told previously that when defining static variables I should keep their scope as limited as possible, so you will notice that I defined these inside of specific methods instead of at the top of this file.
One thing that is bothering me though is the fact that I have other classes observing for these notifications and also accessing the passed userInfo
dictionary when the notification posts. I basically just copy/pasted the notification name's static string and userInfo
dictionary key string.
This is definitely wrong, and I feel like these variables might need to be placed into a single file that would be included in the precompiled header, but I'm not sure if this is the correct approach so any advice on this would be greatly appreciated.
I want to make sure I am following best practices.
NSNotificationCenter
for this. – nhgrif Aug 26 '14 at 21:45