Rompere i servizi nelle rispettive classi. Anche se una classe ha solo una funzione in essa, aggiungerà molta più leggibilità, e quindi scalabilità ed essere facilmente modificabile in futuro. Attenersi alla filosofia Unix secondo cui ogni classe dovrebbe fare una sola cosa e fare bene quell'unica cosa . Quindi vorrei che una classe gestisse la raggiungibilità di Internet usando il centro di notifica e tre classi per ogni servizio. Ad esempio, ecco una classe singleton che ho usato una volta con 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
Estremamente semplice, ma il servizio individuale è ben disaccoppiato, rendendolo più plug-n-play. Questo era da una vecchia app quindi con il mio xp ora, andrei avanti e implementerò anche le mie funzioni di servizio qui come caricare NSData per guidare o eliminare cartelle / file, ecc.
Stavo anche usando il servizio Google Calendar, ma l'ho mantenuto anche nel suo singleton.
Ecco anche la mia classe Reachability che utilizza Raggiungibilità di Tony Million .
#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
Non evitare di rompere le cose. Invece, al contrario, spezzare le cose il più possibile. Questo aiuta anche con Informazioni nascoste .