My current head scratcher: implementing a model class specifically for service calls to my rails application.
Here is the scenario:
- I have a class named Service that is a subclass of NSObject.
- The implementation file has a few methods defined… lets look at doSignUp.
- I am using AFNetworking to communicate with the api.
- From my SignUpViewController, I create an instance of my Service class and call doSignUp
- The method works, as expected and I receive the proper response from the server.
Now Comes the part I don't fully understand:
- AFNetworking utilizes blocks for its service calls.
- Inside the success block I call a helper method called handleSignUp (also in Service class). This method essentially parses the JSON and I create a new User (NSObject subclass) out of it. The handSignUp method then returns the User object.
At this point I have a new User object and I need to send that object back to my SignUpViewController... How can I do that?
- Should I try to add that object to the AppDelegate and access it from the SignUpViewController? This solution could work to access various global properties but when would the SignUpViewController know when to access it?
- Should I try to add a reference to the SignUpViewController in the Service class? That seems counter productive... I might as well add the method doSignUp and handSignUp to the SignUpViewController. It seems to me like my Service class should not be aware of any other viewControllers.
See below for my code examples:
Service.h
//Service.h
#import <UIKit/UIKit.h>
#import "AFNetworking.h"
@interface Services : NSObject
- (void) doSignUp:(NSMutableDictionary*)params;
@end
Service.m
// Service.m
#import "Services.h"
#import "Config.h"
@implementation Services
- (void) doSignUp:(NSMutableDictionary*)params {
NSURL *url = [NSURL URLWithString:@"http://MYURL.COM"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"signup.json" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self handleSignUp:JSON];
} failure:nil];
[operation start];
}
- (User*) handleSignUp:(NSMutableArray*)json {
User * user = nil;
if ([[json valueForKey:@"success"] isEqualToString:@"true"]) {
// create user here ...
}
return user;
}
SignUpViewController.h
#import "Service.h"
@interface SignUpViewController : UIViewController {
Service * service;
}
@property (nonatomic, strong) Service * service;
SignUpViewController.m
#import "SignUpViewController.h"
@interface SignUpViewController ()
@end
@implementation SignUpViewController
@synthesize service = __service;
- (IBAction) initSignUp:(id)sender {
// create params...
[self.service doSignUp:params];
}
Again, all this code does what it is supposed to do... I just need to know how it should all communicate. How can I alert the SignUpViewController the handleSignUp has been called and a new User object is available?
Thanks for your time, Andre