0
我有運行他們的代碼在後臺兩個方法,和方法1觸發方法2如下:在NSLogs等待異步任務操作完成,並繼續當前的異步任務
+(void)insertAllDataInDatabase{
NSLog(@"1");
NSString *[email protected]"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];
});
}
基礎,我想有:
1
2
3
4
4
4
4
...
5
6
,但我有:
1
6
2
3
2
3
2
3
2
3
...
有沒有辦法訂購EXEC這些方法的使用? 非常感謝您的幫助。
但是同步執行操作會阻塞郵件線程,我希望這個進程在後臺運行。 – androniennn
看我的編輯。這些請求將在後臺線程上同步運行,並且主線程將繼續運行。 – dokkaebi
我現在有不同的輸出:'1,6,2,3,2,3,2,3 ... 4,2,3,4,2,3 ...'我不明白那部分'2 ,3'。爲什麼它沒有執行像'2,3,4,4,4,4,...,5' – androniennn