In each loop, I initialize a connector class with an id that is used to perform a JSON call. The problem is, is that this loop continues to iterate before the connectionDidFinishLoading delegate method of the connector class completes, parses the JSON as needed then uses a delegate method with information that it retrieved.
for(NSDictionary *item in views){
NSInteger myID = [[item valueForKey:@"id"] integerValue];
//filter contains these two dictionaries
NSDictionary *ownerDictionary = [filterDictionary valueForKey:@"owner"];
NSString *ownerDisplayName = [ownerDictionary valueForKey:@"displayName"];
self.projectName = projectName;
self.ownerName = ownerDisplayName;
//value inside dictionary for owner
NSString *ownerDisplayName = [ownerDictionary valueForKey:@"displayName"];
//I initialize the connector class here
self.sprintConnector = [[SprintConnector alloc] initWithId:myID];
[self.sprintConnector setDelegate:self];
//**I do not want to continue this loop until the delegate method that i implement is called**/
}
//implementation of delegate method
-(void)didFinishLoadingStuff:(MyObject *)obj{
Project *newProject = [[Project alloc] init];
newProject.projectName = self.projectName;
newProject.projectOwner = self.ownerName;
newProject.sprint = sprint;
//Ok now i have the information that i need, lets continue our loop above
}
//The method of connector class to set up the request is here:
-(void)retrieveSprintInfoWithId{
NSURLConnection *conn;
NSString *urlString = @"myJSONURL";
NSURL *url = [NSURL URLWithString:[urlString stringByAppendingString:self.ID]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"data coming from request: %@", data );
[self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"we finished loading");
NSError *error = nil;
self.projectsDict = [[NSDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:&error]];
NSLog(@"Our sprint array with id: %@ %@", self.ID, self.projectsDict);
//this is where we parse the JSON then use the delegate method that the above class will implement
[self parseJSON];
}
-(void)parseJSON{
[self.delegate didFinishLoadingStuff:SomeObject];
}
I want to be able to force the connectionDidFinishLoading method to be called ->parseJSON->delegate method and for the implementation of that method as mentioned above to complete before the loop continues.
What are my options? Best practices? etc?