0

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.

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.

The request call:

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)
 }                    

And my 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")

}

The cancelWeb:

func cancelWeb(){

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

}

My webViewDidFinishLoad:

 func webViewDidFinishLoad(webView: UIWebView) {

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

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

        timer2.invalidate()
...
...

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.

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?

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.

Thanks fellas!

2
  • Why did you send a request with [NSURLConnection sendAsynchronousRequest] in the first place? If you want to execute a javascript method in an html page, you can use [UIWebView stringByEvaluatingJavaScriptFromString:] Commented Feb 8, 2015 at 1:45
  • It's just one of the thigns I was trying, but is the same effect withouth sendAsynch...I know about the stringByEval...but the case is the same, how can I stop the script if is taking more than X seconds? Is the problem I'm facing (i will update the code about the sendAsynch just in case) Commented Feb 8, 2015 at 1:48

2 Answers 2

0

Another UIWebView delegate func is missing in your code. You may want to add timer2.invalidate() in

func webView(_ webView: UIWebView,
 didFailLoadWithError error: NSError)
Sign up to request clarification or add additional context in comments.

3 Comments

I think you put the wrong code in your answer? You are just writing the same code as I have (you said: instead of using scheduledTimerWithTimeInterval, try)...but your are showing me the same. About the didFailLoadWithError...that's not correct, because is "loading" correctly, the thing is taking long time, but no errors in the request. Thanks
when you say it's taking too long, does it mean the request won't time out after the 5 seconds? What I'm trying to say is if you add log in the func webView(_ webView: UIWebView, didFailLoadWithError error: NSError) will that say anything for debug?
nothing at all, the javscript just process the input (which could take minutes), and send the answer as result, is the only output
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.