我希望它们执行三种方法,如下所示:
+(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
}
});
}
我所拥有的总是数据库中的随机数据,此外,并非所有数据都被存储。我的问题是如何组织这些异步任务以在数据库中获取正确的数据。非常感谢您的帮助。
编辑:
+(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];
}