I have three methods that I want them to execute like follows:
+(void)method1{
// Some code
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=0; i<count; i++) {
//getting object(x) from json
[self method2:x];//trigger method2
}
});
}
+(void)method2:(NSString*)x{
// Some code
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=0; i<count; i++) {
//getting objects(y,z) from json
//SAVE that object in SQLite database
[self method3:y:z];//trigger method3
}
});
}
+(void)method3:(NSString*)y :(NSString*)z{
// Some code
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=0; i<count; i++) {
//getting object from json
//SAVE that object in SQLite database
}
});
}
What i have is always a random data in database, plus, not all data is stored. My question is how to organize those async tasks to get the right data in database. Thank you very much for your help.
EDIT:
+(void)getData:(NSString*)artist{
LKDBHelper* globalHelper = [LKDBHelper getUsingLKDBHelper];
NSMutableArray *arrayOfSongs=[[NSMutableArray alloc]init];
artist = [artist stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//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) {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *arrayOfJson=JSON;
for (int i=0; i<[arrayOfJson count]; i++) {
//DLog(@"artist songs loop");
NSMutableDictionary *songDico=[arrayOfJson objectAtIndex:i];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [Song class]];
Song *song = [parser parseDictionary:songDico];
song.artist=artist;
[globalHelper insertToDB:song];
[self getLyricsWhereArtist:artist andSong:song.song];
}
});
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
NSError *error, id JSON) {
DLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
}