Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in iOS, If I create multiple instances of Erik and each one loads a URL, it raises an ErikError.timeOutError #54

Open
limura opened this issue Jul 28, 2020 · 0 comments

Comments

@limura
Copy link

limura commented Jul 28, 2020

Hello. Thanks for the great library.

It seems that if I create multiple instances of Erik and each one loads a URL, it raises an ErikError.timeOutError.
Running the following code on my iPad mini 4 raises an ErikError.timeOutError in most cases.

    let erikPool:[Erik] = [
        Erik(), Erik(), Erik(), Erik(), Erik(),
        Erik(), Erik(), Erik(), Erik(), Erik(),
    ]
    func testErik() {
        guard let url = URL(string: "https://www.google.com/") else { return }
        func startErik(id:String, url:URL, client:Erik) {
            client.visit(url: url) { (doc, err) in
                if let err = err {
                    print("Erik error.", err.localizedDescription)
                    return
                }
                if let firstLine = doc?.innerHTML?.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: "\n").first {
                    print(id, "request done.", firstLine)
                }else{
                    print(id, "request done. but no content?")
                }
            }
        }
        for (i, erik) in erikPool.enumerated() {
            startErik(id: String(format: "test #%d", i), url: url, client: erik)
        }
    }

Perhaps it's because the handleLoadRequestCompletion in Erik's WebKitLayoutEngine is busy looping, but it looks like the processing isn't up to speed.
As a workaround to this issue, I added Thread.sleep(forTimeInterval: 0.1) to Erik's source code as shown below, and found that each request completed successfully.

    fileprivate func handleLoadRequestCompletion(completionHandler: (Error?) -> Void) {
        // wait load finish
        let condition = pageLoadedPolicy.continueCondition
        let max = Date().timeIntervalSince1970 + pageLoadTimeout
        while(condition(self)) {
            if pageLoadTimeout > 0 && Date().timeIntervalSince1970 > max  {
                completionHandler(ErikError.timeOutError(time: pageLoadTimeout))
                return
            }
        #if os(OSX)
            RunLoop.current.run(mode: RunLoop.Mode.default, before: Date.distantFuture)
        #else
            Thread.sleep(forTimeInterval: 0.1)
        #endif
        }
        completionHandler(nil)
    }

I wasn't sure why I was using a loop to monitor this part of the code, so I decided to use the above workaround. However, you may be able to change this problem to something like a WKWebView.addObserver that monitors changes in "estimatedProgress" or "loading" without using a busy loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant