Delay in making http requests using Alamofire in playground

不问归期 提交于 2019-12-13 04:47:55

问题


I am running into a strange issue where Alamofire.request(.GET) statement in my playground gets executed after some delay in the playground

Setup: I followed the following link to import Alamofire framework to test networking requests in xcode playground.

This is the code I have in my playground. And when I look at the logs of my webserver the logs get updated after almost ~few minutes delay. I have verfied that it is not the log process that is causing delay. Making same http request using curl and from browser, I see logs getting updated almost instantly.

    import UIKit

    import Alamofire



    Alamofire.request(.GET, "http://localhost:5010/asdf")
        .responseJSON { response in
            print ("Hello there in playground")
            print(response.request)  // original URL request
            print(response.response) // URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
    }

回答1:


Playground behavior for time-delayed things like network requests is … unpredictable at best.

Try letting the playground know it should wait for your network request:

import UIKit
import Alamofire

import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

Alamofire.request(.GET, "http://localhost:5010/asdf")
    .responseJSON { response in
        print ("Hello there in playground")
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }

        XCPlaygroundPage.currentPage.finishExecution()
    }


来源:https://stackoverflow.com/questions/33535968/delay-in-making-http-requests-using-alamofire-in-playground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!