【问题标题】:Delay in making http requests using Alamofire in playground在操场上使用 Alamofire 发出 http 请求的延迟
【发布时间】:2015-11-05 03:05:14
【问题描述】:

我遇到了一个奇怪的问题,我的 Playground 中的 Alamofire.request(.GET) 语句在 Playground 中延迟一段时间后被执行

设置:我按照以下link 导入 Alamofire 框架以在 xcode 游乐场测试网络请求。

这是我在操场上的代码。当我查看我的网络服务器的日志时,日志会在几乎几分钟的延迟后更新。我已经验证不是导致延迟的日志过程。使用 curl 和浏览器发出相同的 http 请求,我看到日志几乎立即更新。

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

【问题讨论】:

    标签: xcode swift alamofire swift-playground


    【解决方案1】:

    网络请求等延时事件的 Playground 行为……充其量是不可预测的。

    尝试让 Playground 知道它应该等待您的网络请求:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多