2014-03-31 380 views
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這些方法的使用? 非常感謝您的幫助。

回答

0

您已經從後臺線程調用getArtistSongs:。如果您希望這些連續運行,只需從該方法中刪除dispatch_async調用即可。您還需要同步製作這些請求;我沒有使用AFNetworking,所以我不知道這是否可用或如何去做。

這樣會起作用,而不會阻塞主線程,因爲你要getArtistSongs:來電來人從對後臺線程運行塊:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ 

    // All this code runs in the background. 

    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]; 
    } 

    // All code above runs in the background. 

}); 

6,當然,仍然會立即1後打印。如果你需要代碼來運行最後一個代碼,它會在songDiscos的for循環之後進入代碼塊,可能會被dispatch_async包裝到主線程中。

+0

但是同步執行操作會阻塞郵件線程,我希望這個進程在後臺運行。 – androniennn

+0

看我的編輯。這些請求將在後臺線程上同步運行,並且主線程將繼續運行。 – dokkaebi

+0

我現在有不同的輸出:'1,6,2,3,2,3,2,3 ... 4,2,3,4,2,3 ...'我不明白那部分'2 ,3'。爲什麼它沒有執行像'2,3,4,4,4,4,...,5' – androniennn