Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are really <em>two</em> ways to go about this. </p> <ol> <li><strong>Create a <em>delegate</em> interface</strong></li> <li><strong>Use Blocks</strong></li> </ol> <p>I would strongly advise against using the synchronous methods - unless you are/have created your own asynchronous framework around them (i.e. you are manually starting another thread and executing your synchronous request on that thread). In the long run you will realize you need the requests to be async, and you'll have to re-work everything such that they are. </p> <p>To give a quick overview of the two options I gave: </p> <h2>1. Create a <em>delegate</em> interface</h2> <p>The idea here is to create a class which performs the request, and create a protocol the <em>caller</em> must implement. When the request is complete, you will invoke a specified method on the delegate with the data: </p> <p>The protocol might look something like this:</p> <pre><code>@protocol RequestClassDelegate &lt;NSObject&gt; - (void)requestCompleted:(ResponseClass *)data; - (void)requestError:(NSError *)error; @end </code></pre> <p>The class which makes the request might look something like this:</p> <pre><code>@interface RequestClass : NSObject - (void)makeRequest:(id&lt;RequestClassDelegate&gt;)delegate; @end </code></pre> <p>And the request class implementation might contain some of the following, in addition to your connection logic:</p> <pre><code>@implementation RequestClass { __weak id&lt;RequestClassDelegate&gt; _delegate; } // Connection Logic, etc. - (void)makeRequest:(id&lt;RequestClassDelegate&gt;)delegate { _delegate = delegate; // Initiate the request... } -(void) connectionDidFinishLoading:(NSURLConnection *) connection { NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; // Processing, etc. // Here we'll call the delegate with the result: [_delegate requestCompleted:theResult]; } @end </code></pre> <h2>2. Use Blocks</h2> <p>This solution is much the same as the first solution - but, a bit more elegant in my opinion. Here, we'll change the <code>RequestClass</code> to use blocks instead of a delegate:</p> <pre><code>typedef void (^requestCompletedBlock)(id); typedef void (^requestErrorBlock)(NSError *); @interface RequestClass : NSObject @property (nonatomic, copy) requestCompletedBlock completed; @property (nonatomic, copy) requestErrorBlock errored; - (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error; @end </code></pre> <p>And the implementation of that might look something like this:</p> <pre><code>@implementation RequestClass @synthesize completed = _completed; @synthesize errored = _errored; // Connection Logic, etc. - (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error { self.completed = completed; self.errored = error; // Initiate the request... } -(void) connectionDidFinishLoading:(NSURLConnection *) connection { NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; // Processing, etc. // Here we'll call the delegate with the result: self.completed(theResult); } @end </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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