Note that there are some explanatory texts on larger screens.

plurals
  1. POusing connectiondidFinishLoading in an instance and return data to another view
    text
    copied!<p>i'm quite a beginner at cocoa an objective-c - so please forgive me a possibly trivial question. </p> <p>I'm currently working on an XCODE-Project which is fetching some data via NSJSONSerialization and store it for further work. </p> <p>At this step I'm going to encapsulate the progress of fetching the data into an class which has some setter for the needed parameters (url to fetch from and the layer which should be parsed into an array). In order to use this procedure im creating the method inside this class which creates a connection and a request and returns the array which <em>should</em> contain the data. After some tests I tried to create an instance of this class and called the method which starts to fetch the data.</p> <p>My problem is, that after calling the method data_array from my new instance "block_stats" and store the data in an array of the same type - the array is empty</p> <pre><code>table_data = [block_stats data_array]; </code></pre> <p>The reason of this behavior is that the usage of the methods in (didReceiveResponse,didReceiveData,connectionDidFinishLoading) are working asynchron and the return of the data_array was done before the download was finished.</p> <hr> <p>the method inside the class which contains the downloading part:</p> <pre><code>- (NSMutableArray *)data_array { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; if(data_array) { [data_array removeAllObjects]; } else { data_array = [[NSMutableArray alloc] init]; } NSURLRequest *request = [NSURLRequest requestWithURL:data_url]; connection = [NSURLConnection connectionWithRequest:request delegate:self]; if(connection) { webdata = [[NSMutableData alloc]init]; } return data_array; } </code></pre> <hr> <p>the IBAction in another view which creates the instance and calls the method to fetch the data</p> <pre><code>- (IBAction)refresh:(UIBarButtonItem *)sender { KDJSONparser *block_stats = [[KDJSONparser alloc]init]; [block_stats setURL:[NSURL URLWithString:@"*JSONDATA_URL*"]]; [block_stats setLayer:@"blocks"]; table_data = [block_stats data_array]; } </code></pre> <hr> <p>I would be very glad if anyone could give some advice. It would be very nice if it would be as easy as possible to understand for me. Thanks in advance! </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