I’m trying to think up a scenario when I’d have three different forms of NSData (or whatever) to be sent to three different services like Evernote, Google drive, Dropbox. Granted, each of those has there own SDKs to be bundled into the iOS app, and thats okay.
Now, I want to create a single Class that serialises the both sending and receiving data to and from the above services.
My mind is taking me to a singleton
, a shared class with NSOperationQueue
at its foundation.
I’d periodically add {send:"this data" to:dropbox}
into an NSOperationBlock
and add it to the singleton
.
At any given time say the following operations could be within the singleton
‘s NSOperationQueue
.
{send:"this data" to:dropbox}
{send:"this number" to:evernote}
{send:"this dude" to:googledrive}
The singleton
would need to handle internet reachability, app sessions etc. This class would be connected to Dropbox, Evernote and GDrive and their respective SDK bundles. So, in reality the only thing this class would do is, hand over the data to the respective SDKs to handle it on their own (which is like saying, handle it and let me know if it was a success or failed.)
I’d really like suggestions/critique on the approach i’m taking. I have little experience with NSOperationQueue
, I don’t know if I’m making the right choices here. Some clues to a good API pattern would really be helpful.
UPDATE
It was suggested I create a wrapper for each service. But If can stress that theres little to do in a Dropbox Wrapper
classes, Since in iOS everything is handled in their own SDK. For example, Evernote extends only a simple [ENSession .. uploadNote..]
. All I wanted to do, is serialise my uploads into a single global queue and then dispatch the single methods to either Dropbox
or Evernote
or wherever. If suppose there is a service that requires some form of data handling, I’d then use a wrapper and dispatch its own thread. I’ll add this into the question too. Its the serialising & queueing I want.Sounds reasonable?
4
Break the services into their own respective classes. Even if a class only has one function in it, it will add much better readability, and thus scalability and be readily editable in the future. Stick to the Unix philosophy that each class should do only one thing, and do that one thing well. So I would have one class handle internet reachability using the notification center and three classes for each service. For example, here is a singleton class I once used with Google Drive.
#import <Foundation/Foundation.h>
#import "GTLDrive.h"
#import "GTMOAuth2Authentication.h"
#import "VSGConstants.h"
@interface VSGGoogleServiceDrive : GTLServiceDrive
/**
Static method for having a shared Google service across the app
*/
+ (VSGGoogleServiceDrive *)sharedService;
@end
#import "VSGGoogleServiceDrive.h"
@implementation VSGGoogleServiceDrive
+ (VSGGoogleServiceDrive *)sharedService {
static VSGGoogleServiceDrive *sharedServiceDrive = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedServiceDrive = [[VSGGoogleServiceDrive alloc] init];
});
return sharedServiceDrive;
}
- (id)init {
self = [super init];
if (!self){
return nil;
}
GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init];
auth.accessToken = GOOGLE_ACCESS_TOKEN;
auth.refreshToken = GOOGLE_REFRESH_TOKEN;
auth.tokenURL = [NSURL URLWithString:GOOGLE_TOKEN_URL];
auth.clientID = GOOGLE_CLIENT_ID;
auth.clientSecret = GOOGLE_CLIENT_SECRET;
self.authorizer = auth;
return self;
}
@end
Extremely simple, but the individual service is nicely decoupled, making it more plug-n-play. This was from an old app so with my xp now, I would go ahead and also implement my service functions here such as uploading NSData to drive or deleting folders/files, etc.
I was also using the Google Calendar service, but kept that in it’s own singleton as well.
Here’s also my Reachability class using Tony Million’s Reachability.
#import "Reachability.h"
#import "VSGConstants.h"
/**
This class is used to monitor network connection. Dependent on the Reachability class. Provides an
alert view to the user detailing the network connection upon network changes (disconnect/connect).
*/
@interface VSGNetworkMonitor : NSObject
/**
Network used to check whether there is a valid connection to the server
*/
@property Reachability *network;
- (void)startMonitoringNetwork;
@end
#import "VSGNetworkMonitor.h"
@interface VSGNetworkMonitor()
- (void)checkNetworkStatus:(NSNotification *)notice;
@end
@implementation VSGNetworkMonitor
static NSString *CONNECTION_LOST_TITLE = @"Connection Lost";
static NSString *CONNECTION_LOST_MESSAGE = @"Unable to connect to the server. Please check your connection";
static NSString *NO_CONNECTION_TITLE = @"Network Issue";
static NSString *NO_CONNECTION_MESSAGE = @"No valid internet connection detected";
static NSString *CONNECTION_FOUND_TITLE = @"Connection Found";
static NSString *CONNECTION_FOUND_MESSAGE = @"Connection to the server restored";
- (id)init {
self = [super init];
if(!self) {
return nil;
}
self.network = [Reachability reachabilityWithHostname:SERVER_IP];
if([self.network currentReachabilityStatus] == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NO_CONNECTION_TITLE
message:NO_CONNECTION_MESSAGE
delegate:self
cancelButtonTitle:OK_BUTTON
otherButtonTitles:nil];
[alert show];
}
return self;
}
- (void)startMonitoringNetwork {
[self.network startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
NetworkStatus internetStatus = [self.network currentReachabilityStatus];
switch (internetStatus) {
case NotReachable: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:CONNECTION_LOST_TITLE
message:CONNECTION_LOST_MESSAGE
delegate:nil
cancelButtonTitle:OK_BUTTON
otherButtonTitles:nil];
[alert show];
break;
}
case ReachableViaWiFi:
case ReachableViaWWAN: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:CONNECTION_FOUND_TITLE
message:CONNECTION_FOUND_MESSAGE
delegate:nil
cancelButtonTitle:OK_BUTTON
otherButtonTitles:nil];
[alert show];
break;
}
}
}
@end
Don’t avoid breaking things apart. Instead, to the contrary, break things apart as much as you can. This also helps with Information Hiding as well.