0

i'm creating an application that have some Json calls. For this purpose i have created a new method in application delegate class that do these calls. This method is called from different view controller of my application. I would like use the MBProgressHUD during every call. So i have used an asynch call to retrieve Json data and use MBProgressHUD.

  1. Show MBProgressHUD
  2. Do Asynch NSURLConnection Call
  3. Fetch Data (and Hide MBProgressHUD)
  4. Do others operations.

The problem is that on step 4 the Json response is empty. There is a way to test if the step 2 and 3 is finished?

I have to use asynch call because i'm using MBProgressHUD.

 [MBProgressHUD showHUDAddedTo:view animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:linkChiamata]];
        // NSData* data;
        NSArray* json;
        json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        jsonResponse = [json objectAtIndex:0];

        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:view animated:YES];
        });
    });


    //Esito chiamata JSON
    int esitoChiamata;
    esitoChiamata = [[jsonResponse objectForKey:@"codice"] intValue];

jsonResponse is empty for all calls.

Thanks in advance.

Marco Grieco
  • 229
  • 2
  • 6
  • 16

2 Answers2

0

You need to wait until NSURLConnection delegate success/fail method is called before you do other operations.

Can you provide more info on how you do the NSURLConnection call? Here's one way to do it.

Community
  • 1
  • 1
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
0

I think there is two possibilities.

  • You can use NSCondition to block a thread and wait for condition before to continue. A simple example is here and easy to adapt to your solution.
  • Create a function running in background with a complexion block. See my answer here

Hope it will help you.

Community
  • 1
  • 1
zbMax
  • 2,756
  • 3
  • 21
  • 42
  • code inside your `dispatch_asynch` is sequential, and `dataWithContentsOfURL` is a synchronous request. So in this asynchronous block, you will not execute `[MBProgressHUD hideHUDForView:view animated:YES];` before the code before is done. Are you sure to retrieve any data from `dataWithContentsOfURL` ? – zbMax Dec 19 '13 at 15:28
  • Yes, i retrieve data from dataWithContentsOfURL but i have an asynchronous call and so when i use jsonResponse i get an empty object. I suppose that my call is not finished. – Marco Grieco Dec 19 '13 at 15:45
  • Yes your call is not finished at this time because you retrieve your json from a backthread (moreover with low priority). If there is no trouble for it, I suggest you to include the 2 last lines at the end of your first `dispatch_async` block (after the second `dispatch_async` block) – zbMax Dec 19 '13 at 16:11
  • It is trouble. After last 2 rows i have code that works on previous data (example esitoChiamata). – Marco Grieco Dec 19 '13 at 16:16
  • So you could consider adding `NSCondition`s to block your thread until your backthread unlock it. – zbMax Dec 19 '13 at 16:37
  • Can you give me an example?? – Marco Grieco Dec 20 '13 at 23:26
  • Its the first link of my answer – zbMax Dec 23 '13 at 07:35