Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want to know when it's complete and don't really care about any data, simply use the <code>NSNotificationCenter</code> to post a notification and have your view subscribe to it.</p> <p><strong>Delegate</strong> - Post Notification upon completion</p> <pre><code>-(void) connectionDidFinishLoading:(NSURLConnection*)connection { [[NSNotificationCenter defaultCenter] postNotificationName:@"NSURLConnectionDidFinish" object:nil]; } </code></pre> <p><strong>View</strong> - Add observer and run some code when observed</p> <pre><code>-(void) viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourCleanupMethod) name:@"NSURLConnectionDidFinish" object:nil]; } -(void) yourCleanupMethod { // finish up [[NSNotificationCenter defaultCenter] removeObserver:self]; } </code></pre> <p>Now, if you need to pass a simple object back as data you can try loading up the object parameter in your notification like this:</p> <pre><code>[[NSNotificationCenter defaultCenter] postNotificationName:@"NSURLConnectionDidFinish" object:yourDataObject]; </code></pre> <p>Then change your view and cleanup signature like this:</p> <pre><code>-(void) viewDidLoad { // Notice the addition to yourCleanupMethod [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourCleanupMethod:) name:@"NSURLConnectionDidFinish" object:nil]; } -(void) yourCleanupMethod:(NSNotification *)notif { // finish up id yourDataObject = [notif object]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } </code></pre> <p>Now I found myself needing something a little more than this so I ended up creating a singleton to handle all of my requests. Since all of your delegate methods in <code>NSURLConnectionDelegate</code> give you and instance of the <code>NSURLConnection</code> for the specific connection, you can simply store a mutable data object in a dictionary and look it up each time by the connection. From there I have a method signature that takes and object and selector in that I associate with the connection so after everything has wrapped up, I can pass that mutable data object to the requestor by performing the selector on that object. </p> <p>I won't include all of that code here but maybe that will help get you thinking about what is possible. I found that I had a lot of code tied up in making web service calls so wrapping everything up in a singleton gave me a nice clean way of getting data. Hope this helps!</p>
    singulars
    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. 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