Как правильно реализовать класс Services в моем iOS-приложении?

Моя нынешняя идея: реализовать класс модели специально для сервисных вызовов в моем приложении rails.

Вот сценарий:

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.

Теперь перейдем к той части, которую я не совсем понимаю:

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.

На данный момент у меня есть новыйUser объект, и мне нужно отправить этот объект обратно в мойSignUpViewController... Как мне это сделать?

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.

Ниже приведены примеры моего кода:

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];
 }

Опять же, весь этот код делает то, что должен делать ... Мне просто нужно знать, как все должно общаться. Как я могу предупредитьSignUpViewController handleSignUp был назван и новыйUser объект доступен?

Спасибо за ваше время, Andre

Ответы на вопрос(4)

Ваш ответ на вопрос