0

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.

4

1 回答 1

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 中。

于 2014-03-31T18:38:34.020 回答