【问题标题】:Slow performance with NSScanner and GCDNSScanner 和 GCD 性能下降
【发布时间】:2013-02-02 01:12:46
【问题描述】:

我创建了一个方法来解析.m3u8 文件,下载引用的文件,并重新创建一个新的.m3u8 文件以在本地提供以供离线播放。一切正常,但也许我滥用了NSScaner 或Grand Central Dispatch,因为在我看来,该方法应该快速运行并在GCD 中排队下载。但是,该方法需要花费全部时间来运行(在后台,是的,但我希望尽快创建新文件,而不是在所有内容下载完成后)。谁能看到我的瓶颈在哪里?提前致谢。

- (void)beginDownloadAndCreateLocalM3U8FileForLocalPlaybackFromPlaylist:(NSString*)playlist forId:(NSString*)_id withProgressBlock:(void (^)(float))progress withCompletionBlock:(void (^)(id))success
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @autoreleasepool {
            NSString *stringURL = playlist;
            NSURL  *url = [NSURL URLWithString:stringURL];
            NSError *error;
            NSString *stringData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
            NSString *foundData;
            NSScanner *scanner=[NSScanner scannerWithString:stringData];
            NSUInteger counter = 0;
            NSMutableString *m3u8 = [NSMutableString new];
            [scanner scanUpToString:@"#EXTINF" intoString:&foundData];
            [m3u8 appendString:foundData];
            NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web", documentsDirectory]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web", documentsDirectory] withIntermediateDirectories: YES attributes:nil error: &error];
            }
            if (![self directoryExistsAtAbsolutePath:[NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id]]) {
                [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/Web/%@", documentsDirectory, _id] withIntermediateDirectories: YES attributes:nil error: &error];
            } else {
                success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
                return;
            }
            while (![scanner isAtEnd]) {
                [scanner scanUpToString:@"\n" intoString: &foundData];
                if ([foundData hasPrefix:@"#EXTINF:"]) {
                    [m3u8 appendFormat:@"%@\n", foundData];
                } else if ([foundData hasPrefix:@"http:"]) {
                    NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];
                    if ( urlData )
                    {
                        NSString  *filePath = [NSString stringWithFormat:@"%@/Web/%@/%i.ts", documentsDirectory, _id, counter];
                        NSString *localURL = [NSString stringWithFormat:@"http://127.0.0.1:12345/%@/%i.ts", _id, counter];
                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                            @autoreleasepool {
                                [urlData writeToFile:filePath atomically:YES];
                            }
                        });
                        //assemble the m3u8 file here for each entry with the original durations.  No need to recalculate.
                        //NSLog(@"Adding this: %@", filePath);
                        [m3u8 appendFormat:@"%@\n", localURL];
                        counter++;
                    }
                }
            }
            [m3u8 appendString:@"#EXT-X-ENDLIST"];
            [m3u8 writeToFile:[NSString stringWithFormat:@"%@/Web/%@.m3u8", documentsDirectory, _id] atomically:YES encoding:NSUTF8StringEncoding error:&error];
            NSLog(@"Final m3u8 is %@", m3u8);
            success([NSString stringWithFormat:@"http://127.0.0.1:12345/%@.m3u8", _id]);
        }
    });
}

【问题讨论】:

    标签: ios objective-c grand-central-dispatch


    【解决方案1】:

    这里有这一行

    NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:foundData]];
    

    ...在您的while 循环中与您的扫描仪同步运行,您的扫描仪在下载的 URL 下载之前无法进入下一个令牌。尝试提升低优先级 dispatch_async() 以涵盖 else 案例的所有逻辑,而不仅仅是 writeToFile: 调用。

    【讨论】:

    • 当然!谢谢@iluvcapra。对事物进行第二次观察总是好的。
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    相关资源
    最近更新 更多