Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are starting an asynchronous request for a URL which will take some time. Although it returns immediately, it doesn't imply that the data has been download. <code>NSURLRequest</code>'s delegate will be notified when the data has finished downloading. It is not until then that there is data in <code>mStringData</code> which is probably <code>nil</code> prior to being assigned the downloaded data. So when you do <code>SpriingTxt.text = spriing.mStringData;</code> immediately after an asynchronous request without the data being downloaded, <code>SpriingTxt.text</code> is assigned <code>nil</code>. </p> <p>To resolve this, you can either make a synchronous request which will block until the data has been downloaded which is generally a bad idea or you can message via delegates or notifications to your view controller when the data of your asynchronous request has been downloaded.</p> <p><strong>To implement the delegate</strong></p> <p>Delegates are implemented using protocols. You will create a <code>delegate</code> property in the delegating object which would be <code>Spriing</code> as it will let the delegate know when the string has been downloaded and the view controller will be its delegate as it wants to know when the data is available so that it can update its view. Delegates are usually not retained as most times it is the object that creates them that becomes its delegate. So retaining the delegate would create a retain cycle in such instances. There are lots of tutorials about creating the delegates. A rough implementation would be,</p> <p>in <code>Spriing.h</code></p> <pre><code>@protocol SpriinDelegate; @interface Spriing:... { id&lt;SpriingDelegate&gt; delegate; ... } @property (nonatomic, assign) id&lt;SpriingDelegate&gt; delegate; ... @end @protocol SpriingDelegate - (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString; @end </code></pre> <p>in <code>Spriing.m</code></p> <pre><code>@implementation Spriing @synthesize delegate; ... //When its done - (void)connectionDidFinishLoading:(NSURLConnection *)connection { ... self.mStringData = [self processData:mStringData]; if ( self.delegate &amp;&amp; [self.delegate respondsToSelector:@selector(spriing:didFinishDownloadingString:)]) { [self.delegate spriing:self didFinishDownloadingString:self.mStringData]; } [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } ... @end </code></pre> <p>in the view controller,</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; spriing = [Spriing new]; spriing.delegate = self; [spriing downloadData:@"http://www.spriing.co.uk/services/"]; } - (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString { SpriingText.text = aString; } ... </code></pre>
    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. 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