Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone YouTube API获取暂时冻结UITableView_Iphone_Objective C_Performance_Uitableview_Youtube Api - Fatal编程技术网

Iphone YouTube API获取暂时冻结UITableView

Iphone YouTube API获取暂时冻结UITableView,iphone,objective-c,performance,uitableview,youtube-api,Iphone,Objective C,Performance,Uitableview,Youtube Api,我使用YouTube API非常简单地获取一些视频,并在我的iPhone应用程序的UITableView中显示它们,但是在获取视频时,它会导致我的表暂时冻结 我可以调用下面的代码来获取接下来的25个视频,效果非常好,但在获取过程中的某个时刻,我的UITableView会冻结,然后在几秒钟后,一旦获取完成,它将再次启动。你有什么办法让我阻止这一切吗 NSURL *url = [[[feeds lastObject] nextLink] URL]; GDataQueryYouTube

我使用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方法的第一部分,然后在后面的部分将我的图像对象添加到我的数组中,但是我的数组结果是空的

下面是一个特别缓慢的方法示例,即获取W缩略图。我已经看到并尝试过,但未能将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];
    }
}
-(void)setThumbnailsArray
{
对于(int i=0;i<[[feed entries]count];i++)
{
GDataEntryBase*条目=[[feed entries]objectAtIndex:i];
NSArray*缩略图=[[(gdataentryoutubevideo*)条目媒体组]媒体缩略图];
NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:1]URLString]];
UIImage*image=[UIImage imageWithData:data];
//无缩略图,使用头像创建占位符
如果(图像==nil)
{
UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,0,110,64)];
[查看setBackgroundColor:[UIColor whiteColor]];
UIImageView*avatar=[[UIImageView alloc]initWithImage:[UIImageName:[NSString stringWithFormat:@“%@avatar.png”,memberName]];
[avatar setFrame:CGRectMake(0,0,avatar.frame.size.width-10,avatar.frame.size.height-10)];
avatar.center=view.center;
[视图添加子视图:化身];
//使用化身从视图中创建UIImage
UIGraphicsBeginImageContextWithOptions(view.bounds.size,view.不透明,0.0);
[view.layer RenderContext:UIGraphicsGetCurrentContext()];
UIImage*img=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
图像=img;
}
[feedThumbnails添加对象:图像];
}
}

使用Instruments中的Time Profiler评测应用程序,并了解冻结期间发生的情况。代码看起来没有执行任何会阻止的操作。

您是否在主线程上调用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]调用分离。然后,在工作线程完成处理之前,主线程尝试获取“新”数组。看看我在这里写的关于如何实现简单通知方案的答案。