dispatch_async UIWebView loadrequest [英] dispatch_async UIWebView loadrequest

查看:85
本文介绍了dispatch_async UIWebView loadrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载我的应用程序中的javascript,该脚本将尝试找到解决方案,这可能需要一些时间。我不想等待超过5秒的解决方案,在这种情况下,我只想停止请求并向用户显示一条消息。

I'm loading a javascript which is in my App, the script will try to find a solution, which can takes some time. I don't want to wait more than 5 seconds for that solution, in that case I just want to stop the request and show a message to the user.

我'我一直试图用NSTimers和dispatch_async来做,但是UIWebView请求仍在使用我的整个主队列,而NSTimer在此期间停止了我正在等待答案。

I've been trying to do it with NSTimers and with dispatch_async, but the UIWebView request is still using my whole main queue, and the NSTimer is stopped in the meantime I'm waiting for the answer.

请求电话:

webViewUIView.delegate = self

var path = NSBundle.mainBundle().pathForResource("page", ofType: "html")

var url = NSURL(fileURLWithPath: path!)
var req = NSURLRequest(URL:url!)
var queue = NSOperationQueue()

 dispatch_async(dispatch_get_main_queue()) {
    println("********CALLING TO LOADREQUEST")

    self.webViewUIView.loadRequest(req)
 }                    

我的NSTimer:

var timer2 = NSTimer()

func webViewDidStartLoad(webView: UIWebView) {
    println("********Creating the Timer")
    let aSelector : Selector = "cancelWeb"
    self.timer2 = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: aSelector, userInfo: nil, repeats: false)
    println("After create the timer")

}

cancelWeb:

The cancelWeb:

func cancelWeb(){

        println("****Before Stop Load")
        webViewUIView.stopLoading()
        println("After stopLoading")
        timer2.invalidate()

}

我的webViewDidFinishLoad:

My webViewDidFinishLoad:

 func webViewDidFinishLoad(webView: UIWebView) {

        webViewUIView.stringByEvaluatingJavaScriptFromString("func('\(obj)')")

        let result: String = webViewUIView.stringByEvaluatingJavaScriptFromString("document.getElementById('log').innerText")!

        timer2.invalidate()
...
...

当我运行应用程序时,即使在Timer中设置了5秒后,该过程仍在运行,我只看到这个meesage:关于创建TImer,After Timer和CALLING TO LOADREQUEST。

When I run the app, the process is still running, even after the 5 seconds setted in the Timer, I only see this meesage: about Creating TImer, After Timer, and CALLING TO LOADREQUEST.

有关如何与NSTimer同时并行运行请求的任何想法?正如您所看到的,调度不起作用,可能是因为URL在同一个Bundle中?

Any ideas about how to run the request in parallel at the same time with the NSTimer? As you see the dispatch is not working, maybe because the URL is in the same Bundle?

嗯,在此期间我正在等待解决方案,App是冻结,只是等待答案...这可能需要花费很长时间。

Well, in the meantime I'm waiting for the solution, the App is frozen, just waiting for the answer...which could take loooong time.

在请求中使用超时是行不通的,因为连接是正确建立的,事情是这是需要很长时间才能收到答案。

Using the timeout in the request is not working, because the connection is stablished correctly, the thing is that is taking long time in receive the answer.

谢谢fellas!

推荐答案

好的,我在我的Javascript代码中创建了一个Worker,所以我可以像新线程一样处理它,但不能在iOS中处理(使用UIWebView是不可能的)。解决方案如下:终止后的iOS Javascript Workers高CPU()

Ok guys, I created a Worker in my Javascript code, so I can handle it like new threads but not in iOS (which was impossible to do using UIWebView). The solution is here: iOS Javascript Workers High CPU after terminate()

这篇关于dispatch_async UIWebView loadrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆