Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:55:28+00:00 2026-06-05T23:55:28+00:00

My current head scratcher: implementing a model class specifically for service calls to my

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:55:29+00:00Added an answer on June 5, 2026 at 11:55 pm

    As far as i can see you are not realizing the asynchronous nature of this process: actually since network or IO operations take a long time to complete we tend to prefer asynchronous access, i mean: the caller (View Controller) calls a message on the web API through the service and then STOPS WAITING FOR A RESPONSE. Actually the network processing could be done on a different process or thread or processor’s core.

    Now then, how can the caller BE NOTIFIED by the service when the operation completes?
    For this to happen we must bind the two objects using Delegation:
    we create a protocol (it’s just a declaration of messages that an object will be responding to) and we implement it in our View Controller, that way whoever will be using it will know for sure what messages are available.

    Then we will declare a delegate of type id in our service that will be called once the operations will be completed.

    It’s almost like importing your ViewController.h in the service class and giving the service a pointer to the view controller, but done in the right way (without circular references and respecting SOLID principles)

    Now some code:

    //service.h
    @protocol RemoteApiProtocol <NSObject>
    @required
    -(void) UserLoggingIn;
    -(void) UserLoggedIn:(User*)user;
    -(void) UserLoginError:(NSError *)error;
    @end
    

    The protocol works like a small interface, it’s a contract between two classes/objects.
    Then in your service you can declare field & property for the protocol:

    //Service.h
    #import <UIKit/UIKit.h>
    #import "AFNetworking.h"
    @interface Services : NSObject{
          __weak id<RemoteApiProtocol> delegate;
    }
    
    @property (nonatomic, assign) id<RemoteApiProtocol> delegate;
    
    - (void) doSignUp:(NSMutableDictionary*)params;
    
    @end
    

    At this point you implement the protocol in your view controller, integrating the contract you just build. This way the server will know that the delegate will always be able to respond to the protocol messages.

    //SignUpViewController.h
    #import "Service.h"
     @interface SignUpViewController : UIViewController <RemoteApiProtocol> {
         Service * service;
     }
    
     @property (nonatomic, strong) Service * service;
    

    After implementing the protocol you can assign your view controller as delegate for the server, this way the server, after calling the apis will be able to call the delegate with the result of the call. For this to happen you implement the protocols messages that will be called by the service:

    //SignUpViewController.m
    #import "SignUpViewController.h"
    
     @implementation SignUpViewController
    
     @synthesize service = __service;
     - (IBAction) initSignUp:(id)sender {
          //create the service with params
          //...
    
          //assign self as the delegate
          self.service.delegate = self;
          [self.service doSignUp:params];
     }
    
    #pragma mark - RemoteApiProtocol
    -(void) UserLoggingIn
    {
          //login started, 
          //you can show a spinner or animation here
    }
    -(void) UserLoggedIn:(User*)user
    {
          //user logged in , do your stuff here
    }
    -(void) UserLoginError:(NSError *)error
    {
          NSLog(@"error: %@",error);
    }
    
    @end
    

    And finally you call the messages in your service, after calling the apis in the success and error block:

    // 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];
    
    
         //Signaling the start of the signup operation to the delegate
         if(self.delegate){
              [self.delegate UserLoggingIn];
         }
    
         [operation start];
     }
    
     - (User*) handleSignUp:(NSMutableArray*)json {
         User * user = nil;
         if ([[json valueForKey:@"success"] isEqualToString:@"true"]) {
               // create user here ...
               //call the delegate (view controller) passing the user
               if(self.delegate){
                   [self.delegate UserLoggedIn:user];
               }
         }else{
               //error handling
               if(self.delegate){
                   //NSError *error = ...
                   //[self.delegate UserLoginError:error];
               }
         }
    
    
         //return user;
     }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my current xpath code /html/head/title . But you know, in the real
This one is a real head scratcher... I have a number of command links
I have run into a bit of a problem and a head scratcher, as
Keeping these in mind - HttpContext.Current - Foreach I'm having trouble wrapping my head
I need to insert an HTML string into the <head> tag of the current
Current Process: I have a tar.gz file. (Actually, I have about 2000 of them,
Current Application Server Configuration: IIS 6.0 Windows 2003 Server Standard Edition SP2 .NET Framework
The specs for my rails project have been really slow lately. I did a
Hi I am writing a linked list data type. I have an inner class
If you want to move the HEAD to the parent of the current HEAD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.