0

Imagine that you are getting date from different two URLs, one is for map overlay, another one is for annotation as you can see below.There are two methods (plotAnnotations and plotOverlays) take care of JSON data and plot them on map View. I just want to make sure, is there any other way to do? How do I check/know whether or not annotations and overlays comes at the same time? I would be glad to hear any feedbacks or comments. Thanks in advance.

_weak ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
[request1 setCompletionBlock:^{
    NSString *responseString = [request1 responseString];
    [self plotAnnotations:responseString];

}];
[request1 setFailedBlock:^{
    NSError *error=[request1 error];
    NSLog(@"Error: %@", error.localizedDescription);
}];
[request1 startAsynchronous];

 __weak ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
[request2 setCompletionBlock:^{
    NSString *responseString2 = [request2 responseString];
    [self plotOverlays:responseString2];
}];
[request2 setFailedBlock:^{
    NSError *error=[request2 error];
    NSLog(@"Error: %@", error.localizedDescription);
}]; 

   [request2 startAsynchronous];
casillas
  • 16,351
  • 19
  • 115
  • 215
  • should I use any thread to make sure data would be read at the same time ? [self performSelectorOnMainThread:@selector() withObject:nil waitUntilDone:false]; – casillas Sep 16 '12 at 19:14

1 Answers1

1

You should take a look at GCD (Grand Central Dispatch). GCD docs. Create a dispatch group containing your requests, and you can use this to process both your annotations and overlays when they have both finished downloading - assuming that is what you want to do, it isn't entirely clear, but it's what your comment seems to imply.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41
  • Hello Paul, thanks a lot for giving me feedback. I would definetly look at the GCD documentation and examples are available on the Internet. I just wonder what would happen if I implemented the one that I posted above. What would be the risk? – casillas Sep 16 '12 at 20:34
  • 1
    PerformSelctor is underrated, but it won't really do what you want. The problem is that your calls are asynchronous, and so the two blocks will be executed independently, probably returning after the method is complete - hence why a dispatch group would be good. Converting the calls to synchronous would enable you to group them effectively within one block, but would be inefficient. – Paul Lynch Sep 17 '12 at 00:49
  • Hello Paul, thanks a lot for quick reply again. Just want to sure that in the above code just changing calls from async to sync that would be better, am I right? I just want to test my app first for quick result. Then I would definitely focus on GCD. thanks in advance. – casillas Sep 17 '12 at 01:32
  • 1
    If you change both the calls above to synchronous, you will be able to assume at the end of the code block that both have completed, yes. It would be ok for testing, but I wouldn't want production code to do that - which is where gcd comes in. – Paul Lynch Sep 17 '12 at 09:18
  • Hello Paul, could you take a look at my post please?http://stackoverflow.com/questions/12557553/mkoverlay-adding-removing-overlay – casillas Sep 24 '12 at 03:21
  • 1
    You should look at the Breadcrumbs sample code as suggested, it is very useful. Also, your plotOverlay is drawing additional overlays every time it is called. It will take you several stages of changes to get it to do what you want. Try one thing at a time :-). Also, if you end up using GCD, you can do the slow stuff in the dispatch queues, but must call the main queue to carry out any UIKit/drawing. – Paul Lynch Sep 24 '12 at 19:32
  • Thanks a lot Paul. Could you take a look at my post please, that is the one updated with GCD but not working unfortunately.http://stackoverflow.com/questions/12571920/multiple-asihttprequests – casillas Sep 24 '12 at 20:35