【问题标题】:YouTube API fetch freezes UITableView momentarilyYouTube API 提取会暂时冻结 UITableView
【发布时间】:2012-12-08 01:16:05
【问题描述】:

我正在使用 YouTube API 非常简单地获取一些视频并将它们显示在我的 iPhone 应用程序的 UITableView 中,但是它会导致我的表格在获取视频时暂时冻结。

我可以调用下面的代码来获取接下来的 25 个视频,它运行良好,但是在获取过程中的某个时刻,我的 UITableView 冻结,然后在几秒钟后,一旦获取完成,它就会再次启动。您对我如何阻止这种情况有任何想法吗?

NSURL *url = [[[feeds lastObject] nextLink] URL];
        GDataQueryYouTube* query = [GDataQueryYouTube  youTubeQueryWithFeedURL:url];
        ticket = [service fetchFeedWithQuery:query delegate:self didFinishSelector:@selector(requestAdditional:finishedWithFeed:error:)];



- (void)requestAdditional:(GDataServiceTicket *)ticket finishedWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
{
    [feeds addObject:aFeed];

    NSArray *newEntries = [aFeed entries];

    for (GDataEntryBase *entry in newEntries)
    {
        [entry setParent:nil];
        [feed addEntry:entry];
    }

    [self fetchVideoData];

    [self dismissViewControllerAnimated:YES completion:^{}];

    [self.tableView reloadData];
}

编辑

我认为导致速度变慢的原因是我拨打[self fetchVideoData]; 时。在这个方法中,我调用了一堆其他方法来获取所有不同的数据位、视频标题、观看次数、缩略图等。

我尝试将此方法的大部分内容放在 GCD 方法的第一部分,然后在后半部分将我的图像对象添加到我的数组中,但我的数组只是空的。

这是一个特别缓慢的方法的示例,即获取缩略图。我已经看到并尝试过,但未能将 GCD 方法应用于此。

-(void)setThumbnailsArray
{
    for (int i = 0; i < [[feed entries] count]; i++)
    {
        GDataEntryBase *entry = [[feed entries] objectAtIndex:i];
        NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:1] URLString]]];
        UIImage *image = [UIImage imageWithData:data];

        //No thumbnail, create placeholder using avatar
        if (image == nil)
        {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 64)];
            [view setBackgroundColor:[UIColor whiteColor]];
            UIImageView *avatar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@Avatar.png", memberName]]];
            [avatar setFrame:CGRectMake(0, 0, avatar.frame.size.width - 10, avatar.frame.size.height - 10)];
            avatar.center = view.center;
            [view addSubview:avatar];

            //Create UIImage from the view with the avatar
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            image = img;
        }

        [feedThumbnails addObject:image];
    }
}

【问题讨论】:

    标签: iphone objective-c performance uitableview youtube-api


    【解决方案1】:

    你是在主线程上调用 youtube 吗?如果是这样,这将导致 UI 在执行该调用时变得无响应。

    通常,您永远不想在主线程上执行时间密集型操作。使用 Grand Central Dispatch,它看起来像这样:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Time consuming operations go here
    
        //When finished call back on the main thread:
        dispatch_async(dispatch_get_main_queue(), ^{
            //Return results and update data back on main thread 
        });
    });
    

    【讨论】:

    • 啊,现在我看到了一些使用它的建议,但我无法让它工作。我已经更新了我的问题,也许你可以看看,谢谢。
    • 当然有可能只是时间问题。我可能会考虑在这里也尝试使用通知。我猜正在发生的事情是工作线程被 [self fetchVideoData] 调用剥离。比主线程尝试在工作线程完成处理之前获取“新”数组。查看我在stackoverflow.com/questions/13954003/… 写的关于如何实现简单通知方案的答案。
    【解决方案2】:

    使用 Instruments 中的 Time Profiler 分析您的应用程序,并找出冻结期间发生的情况。代码看起来没有做任何会阻塞的事情。

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2018-05-04
      • 2021-05-03
      • 1970-01-01
      • 2012-07-02
      • 2017-07-04
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      相关资源
      最近更新 更多