如何建立排队系统?

时间:2013-12-13 19:05:00

标签: ios objective-c

Bellow我有一些下载视频的代码。代码从我提供的字符串下载视频,每次调用只能下载一个视频。我的问题是我可以制作一个排队系统,可以在同步后直接启动下一个提供的字符串。 在此先感谢

 -(IBAction)download{



    LBYouTubeExtractor *extractor = [[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:URL.text] quality:LBYouTubeVideoQualityLarge];

    [extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
        if(!error) {

            NSLog(@"Did extract video URL using completion block: %@", videoURL);
            NSURL *url = videoURL;


            [extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {


                if(!error) {




                    NSLog(@"Did extract video URL using completion block: %@", videoURL);


                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                                            filePath = [NSString stringWithFormat:@"%@/Documents/%@.mp4", NSHomeDirectory(),title.text];
                        filename = [NSString stringWithFormat:(@"video_%@.mp4"), videoURL];
                        data = [NSData dataWithContentsOfURL: videoURL];
                        [data writeToFile:filePath atomically:YES];
                        NSLog(@"File %@ successfully saved", filePath);


                    }

                                   );


                }
                else {
                    NSLog(@"Failed extracting video URL using block due to error:%@", error);
                }
            }];

        } else {
            NSLog(@"Failed extracting video URL using block due to error:%@", error);
        }

    }];

}

1 个答案:

答案 0 :(得分:3)

看看NSOperationQueue

创建一个maxConcurrentOperationCount为1的NSOperationQueue是相当简单的,并传递您的队列代码块以使用addOperationWithBlock执行。在您的情况下,您可以遍历视频列表并将每个下载排入您的NSOperationQueue。