I have two methods that are running their code in background, and method1 triggers method2 as follows:
+(void)insertAllDataInDatabase{
NSLog(@"1");
NSString *url=@"http://localhost/kalimat/get_all_artists.php";
//NSLog(@"url %@",url);
NSURL *urlChannels= [ NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlChannels];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
NSMutableArray *arrayOfJson=JSON;
for (int i=0; i<[arrayOfJson count]; i++) {
NSLog(@"2");
NSMutableDictionary *songDico=[arrayOfJson objectAtIndex:i];
NSString *artist=[songDico objectForKey:@"artist"];
[self getArtistSongs:artist];
}
});
NSLog(@"6");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON) {
//DLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
}
+(void)getArtistSongs:(NSString*)artist {
NSLog(@"3");
LKDBHelper* globalHelper = [LKDBHelper getUsingLKDBHelper];
NSMutableArray *arrayOfSongs=[[NSMutableArray alloc]init];
artist = [artist stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//DLog(@"artisttt %@",artist);
NSString *url=[NSString stringWithFormat:@"%@?artist=%@", @"http://localhost/kalimat/get_kalimat.php",artist];
url = [url stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
//NSLog(@"url %@",url);
NSURL *urlChannels= [ NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlChannels];
[LKDBHelper clearTableData:[Song class]];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON) {
NSMutableArray *arrayOfJson=JSON;
for (int i=0; i<[arrayOfJson count]; i++) {
NSLog(@"4");
NSMutableDictionary *songDico=[arrayOfJson objectAtIndex:i];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [Song class]];
Song *song = [parser parseDictionary:songDico];
song.artist=artist;
[arrayOfSongs addObject:song];
//DLog(@"inserting...");
[globalHelper insertToDB:song];
//DLog(@"getting lyrics");
//[self getLyricsWhereArtist:artist andSong:song.song];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"AllArtistsSongs" object:arrayOfSongs];
}
NSLog(@"5");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON) {
DLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
});
}
Basing on the NSLogs, I want to have :
1
2
3
4
4
4
4
...
5
6
But I'm having:
1
6
2
3
2
3
2
3
2
3
...
Is there a way to order the execution of those methods? Thank you very much for your help.