YouTube UITableView cells using UIWebView blocking thread?
I have a UITableView
which displays YouTube data such as video title and date. I am also displaying a UIWebView
(75x75px) for each UITableViewCell
which loads an HTML string.
here is the pertinent code for - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
:
UIWebView *iconView = (UIWebView*)[cell.contentView viewWithTag:2];
[self embedYouTube:videoUrl frame:CGRectMake(0, 0, 75, 75) webView:iconView];
and the embedYouTube
function:
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame webView:(UIWebView*)webView {
if([loadedIconsArray containsObject:webView])
return;
NSString *youTubeVideoHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, url, frame.size.width, frame.size.height];
[webView loadHTMLString:html baseURL:nil];
[loadedIconsArray addObject:webView];
}
loadedIconsArray
is an NSMutableArray
the problem:
When i load up my YouTube view, it seems as though the main thread is blocked while these UIWebView
objects are loading the HTML. after a few seconds, everything is working as expected. I notice开发者_Python百科 that i experience this lag only after installing the application for the first time. all other application/view launches work fine.
Why am i experiencing this "lag" or thread blocking?
How can i avoid it?
To begin with, this is not a problem caused by network access on the main thread (at least not any network access you have control over). The UIWebView method called on the main thread is loadHTMLString:baseURL:, which simply stuffs the specified string into the web view as its HTML content. The contents of the specified HTML string imply a network request to YouTube, but that doesn't create a user-initiated network request on the main thread any more than clicking on a link in a web view.
Any suggestion to perform this operation in a background thread is wrong, and will almost certainly result in a crash. Any interaction with a web view should be in the main thread; iOS will give you a crash error for even instantiating a web view on a background thread.
Unfortunately, and here lies the rub, there may be nothing you can do about this lag. When the UIWebView sees the <embed>
tag in the specified HTML and determines that the content is meant to come from YouTube, it starts up a native YouTube player process to play the video. The lag may be caused by the time the native player takes to start, it may be caused by the native player contacting YouTube, it may be caused by gnomes that live in the native player's basement. I don't know the cause, and since the native player is an entirely opaque process (short of jailbreaking a devices and going at some libraries with a disassembler) I don't have a good suggestion for how to find out. Even if you did find out, there's almost certainly little to nothing you could do about it.
I appreciate that this is an unsatisfying answer, and I'd be happy for someone to prove me wrong.
I am not sure about this, but I thought it might help u so thought of posting this answer ...
can u just inspect and find out that whether "webviewDidLoad
" is called when the video player stops processing the video.
if it really calls what I would recommend doing is working out some other thing till "webviewDidload" is called that is native player stops processing the video like youtube gives a spinning white wheel till it loads the video, and then display the webview.
I hope I have understood ur question properly and this could help u a bit..
Any network access shall NOT be done on the main thread. Use the asynchronous API to load content. In the meantime just provide a simple placeholder image/local-webview to be displayed.
精彩评论