You could:
create an
NSOperationQueue
, andcall
sendAsynchronousRequest
, placing all of yourNSData
processing code inside the completion block.
Thus, instead of:
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// now process resulting `data`
Use:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// now process resulting `data`
}];
Alternatively, you could implement the NSURLConnectionDataDelegate
methods. For more information on that, see the Using NSURLConnection section of the URL Loading System Programming Guide.
You say "if the request is empty": I assume you mean "if the data returned is empty". And you say it is [[]]
. If that's really what you're getting, it sounds like an array with one item (which itself, is an empty array). Or is it []
(which is an empty array)? Or is it nil
?
I'm going to assume that the data returned was []
, an empty array.
I'd also suggest you consider using NSJSONSerialization
, the built in JSON parser, but obviously you can use JSONValue
if you really want.
Finally, your implementation is skipping the first entry (NSArray
uses a zero-based index). I'm assuming that was unintentional.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"%s: sendAsynchronousRequest error: %@", __FUNCTION__, error);
return;
}
NSError *parseError;
NSArray *meinErgebnis = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (parseError) {
NSLog(@"%s: JSONObjectWithData error: %@", __FUNCTION__, parseError);
return;
}
if ([meinErgebnis count] == 0) {
NSLog(@"%s: meinErgebnis empty", __FUNCTION__);
return;
}
for (NSDictionary *dictionary in meinErgebnis)
{
// now process each dictionary entry in meinErgebnis
}
// etc.
}];