xcode - 在 Playground 上使用 Alamofire 延迟发出 http 请求

标签 xcode swift alamofire swift-playground

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

设置:我遵循以下 link导入 Alamofire 框架以在 xcode playground 中测试网络请求。

这是我在 Playground 上的代码。当我查看我的网络服务器的日志时,日志会在大约几分钟的延迟后得到更新。我已经确认不是日志进程导致了延迟。使用 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)")
            }
    }

最佳答案

像网络请求这样的时间延迟的 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()
    }

关于xcode - 在 Playground 上使用 Alamofire 延迟发出 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535968/

相关文章:

xcode - Xcode的导航器 Pane 中的箭头指示什么?

iphone - 如何在不使用按钮的情况下移动到另一个 View

ios - 在 iOS 应用程序中查找图像资源 (XCode 4.2)

swift - “Fatal error: Unexpectedly found nil while unwrapping an Optional value”是什么意思?

ios - 带有嵌入库的 Cocoa Touch Framework Swift

swift - 如何在 Swift5 中使用 ObjectMapper 映射 Alamofire 5.2 响应

xcode - 在后台保存 PFObject

ios - SmartView SDK Samsung/Tizen - 内部服务器错误 500

ios - 在 Swift 框架中使用 Realm 为同样使用 Realm 的主机 iOS 应用程序带来挑战

ios - Xcode 8.0 beta 找不到这样的模块 Alamofire Swift 3.0