Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not a pro, but I've been struggling with the same problem so here's the best I've got so far:</p> <pre><code>@interface MyCallback : NSObject id target; SEL action; id value1; id value2; ... @end </code></pre> <p>When I need to call something that will notify me about completion I fill set target and action with the actual callback (usually "self" and a method) and stuff other values I will need later into value1..2..3 The callee hangs on to the MyCallback object and when time comes it calls the target/action and passes the MyCallback as a parameter. That's how I get my "closure".</p> <p>To address your other problem here's an idea: create a class derived from NSURLConnection, called MYURLConenction, and only addition is a field of type "id", call it MyStuff. Then proceed to use MYURLConenction instead of NSURLConnection - when you make a network request you can stuff your data (such as an instance of MyCallback) into MyStuff and it will hang on to them for you. Therefore you will not need to create a new instance of your service class for each invocation.</p> <p>This is actually not how I do it though - I simply created a generic MyHTTPRequest wrapper class that is the delegate of the NSURLConnection, it accepts a URL &amp; NSString POST body, processes all the network stuff and returns an NSString result and an NSError to the caller - a little bit higher abstraction. It also stores MyStuff, so that service classes can remain singletones if they want to.</p>
 

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