16955


swiftで下記のような関数を作り、WebAPIと通信を試みましたが、この関数の中のsendAsynchronousRequestが実行されずに結果が帰ってきません。プリント文をこのメソッドの中に書き確認しました。

コード

[code]

    func requestList(order: String?, to: NSDate?, from: NSDate?, completion: ((response: NSURLResponse!, data: [Data]?, error: NSError?) -> Void)){
            let dateFormatter = NSDateFormatter()
            dateFormatter.locale = NSLocale(localeIdentifier: "ja_JP")
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"
            var urlStr = baseUrl
            if order != nil{
                urlStr += "\(formatParam)\(orderParam)" + order!
            }else{
                urlStr += "\(formatParam)"
            }
            if to != nil{
                let strTo = dateFormatter.stringFromDate(to!)
                urlStr += "\(toParam)\(strTo)"
            }
            if from != nil{
                let strFrom = dateFormatter.stringFromDate(from!)
                urlStr += "\(fromParam)\(strFrom)"
            }
    
            println(urlStr)
            let req = NSURLRequest(URL: NSURL(string: urlStr)!,
                cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
                timeoutInterval: 60.0)
    
            let queue: NSOperationQueue = NSOperationQueue()
    
            NSURLConnection.sendAsynchronousRequest(req, queue: queue,
                completionHandler: {
                    [unowned self]
                    (response: NSURLResponse!, data: NSData!, error :NSError!) -> Void in
    
                    println("aaa")
                    var jsonResult: NSDictionary? = nil
                    var resultError: NSError? = error
                    var resultData: [Data]?
    
                    if error == nil{
                        jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &resultError) as? NSDictionary
    
                        if resultError == nil && jsonResult != nil{
                            resultData = self.parseDataList(jsonResult!)
                        }
                    }
                    completion(response: response, data: resultData, error: resultError)
                })
        }
    
[/code]

テストは以下のようになっています。

テスト

[code]

    func testRequestList(){
            let dateFormatter = NSDateFormatter()
            dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
            var order = "new"
            var to = NSDate.date(year: 2015, month: 9, day: 20)
            var from = NSDate.date(year: 2015, month: 8, day: 1)
            println(dateFormatter.stringFromDate(to))
            println(dateFormatter.stringFromDate(from))
    
            ApiController.sharedInstance.requestList(order, to: to, from: from, completion: {
                (resp: NSURLResponse!,
                    data: [Data]?,
                    error: NSError?) in
                var errorMessage: String?
    
                println("aaaa")
                if error != nil{
                    println(error!.localizedDescription)
                    return
                }
                println("aaa")
                println(data!)
                for d in data!{
                    println(d.temperature)
                }
    
            })
    
        }
    
[/code]

ブラウザでURLの正しさは確認済みです。Swift初心者なので初歩的な質問かも知れませんがご教授ねがえますでしょうか。よろしくお願いします。


CC BY-SA 3.0
2015-09-26T08:33:27.717
16950
2015-09-26T10:20:18.470
7232
question
swift
SwiftのsendAsynchronousRequestが呼ばれない


以下の変更をしたところうまく動きました。

[code]

    func testRequestList(){
            let dateFormatter = NSDateFormatter()
            dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
            var order = "new"
            var to = NSDate.date(year: 2015, month: 9, day: 20)
            var from = NSDate.date(year: 2015, month: 8, day: 1)
            println(dateFormatter.stringFromDate(to))
            println(dateFormatter.stringFromDate(from))
            //追加
            let expectation = expectationWithDescription("Finish")
    
            ApiController.sharedInstance.requestList(order, to: to, from: from, completion: {
                (resp: NSURLResponse!,
                    data: [Data]?,
                    error: NSError?) in
                var errorMessage: String?
    
                println("aaaa")
                if error != nil{
                    println(error!.localizedDescription)
                    return
                }
                println("aaa")
                for d in data!{
                    println(d.temperature)
                }
                //追加
                expectation.fulfill()
    
            })
            //追加
            self.waitForExpectationsWithTimeout(1, handler: nil)
    
[/code]


CC BY-SA 3.0
2015-09-26T10:20:18.470
16955
2015-09-26T10:20:18.470
7232
16950
answer