延迟在操场上使用Alamofire发出HTTP请求 [英] Delay in making http requests using Alamofire in playground

查看:108
本文介绍了延迟在操场上使用Alamofire发出HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,在操场上经过一段时间的延迟后,操场上的Alamofire.request(.GET)语句得到执行

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

设置:我遵循了按照链接导入Alamofire框架以测试网络

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

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

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


推荐答案

对于诸如网络请求之类的时间延迟事件,操场上的行为充其量是……不可预测的。

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

这篇关于延迟在操场上使用Alamofire发出HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆