Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly implement a Services class into my iOS application?
    primarykey
    data
    text
    <p>My current head scratcher: implementing a model class specifically for service calls to my rails application.</p> <p>Here is the scenario:</p> <ul> <li>I have a class named <strong>Service</strong> that is a subclass of NSObject.</li> <li>The implementation file has a few methods defined… lets look at <em>doSignUp</em>.</li> <li>I am using <strong>AFNetworking</strong> to communicate with the api.</li> <li>From my <strong>SignUpViewController</strong>, I create an instance of my <strong>Service</strong> class and call <em>doSignUp</em></li> <li>The method works, as expected and I receive the proper response from the server.</li> </ul> <p>Now Comes the part I don't fully understand:</p> <ul> <li><strong>AFNetworking</strong> utilizes blocks for its service calls.</li> <li>Inside the <em>success</em> block I call a helper method called <em>handleSignUp</em> (also in <strong>Service</strong> class). This method essentially parses the JSON and I create a new <strong>User</strong> (NSObject subclass) out of it. The <em>handSignUp</em> method then returns the <strong>User</strong> object.</li> </ul> <p>At this point I have a new <strong>User</strong> object and I need to send that object back to my <strong>SignUpViewController</strong>... How can I do that?</p> <ul> <li>Should I try to add that object to the <strong>AppDelegate</strong> and access it from the <strong>SignUpViewController</strong>? This solution could work to access various global properties but when would the <strong>SignUpViewController</strong> know when to access it?</li> <li>Should I try to add a reference to the <strong>SignUpViewController</strong> in the <strong>Service</strong> class? That seems counter productive... I might as well add the method <em>doSignUp</em> and <em>handSignUp</em> to the <strong>SignUpViewController</strong>. It seems to me like my <strong>Service</strong> class should not be aware of any other viewControllers.</li> </ul> <p>See below for my code examples:</p> <p>Service.h</p> <pre><code>//Service.h #import &lt;UIKit/UIKit.h&gt; #import "AFNetworking.h" @interface Services : NSObject - (void) doSignUp:(NSMutableDictionary*)params; @end </code></pre> <p>Service.m</p> <pre><code>// 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; } </code></pre> <p>SignUpViewController.h</p> <pre><code>#import "Service.h" @interface SignUpViewController : UIViewController { Service * service; } @property (nonatomic, strong) Service * service; </code></pre> <p>SignUpViewController.m</p> <pre><code>#import "SignUpViewController.h" @interface SignUpViewController () @end @implementation SignUpViewController @synthesize service = __service; - (IBAction) initSignUp:(id)sender { // create params... [self.service doSignUp:params]; } </code></pre> <p>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 <strong>SignUpViewController</strong> the <em>handleSignUp</em> has been called and a new <strong>User</strong> object is available?</p> <p>Thanks for your time, Andre</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload