2016-07-30 28 views
0

時12小時後,我得到的墜毀報告18次大約一種的HTTPRequest方法。iOS應用崩潰有時處理的HTTPRequest(JSON)

此方法用於從服務器加載歌曲信息,例如歌曲標題,解釋。

的崩潰報告來自Fabric.io,但我不能unterstand非常好,這是一種約的NSOperation和線程....

我的問題是,什麼也使崩潰?以及如何改善此功能loadMetadata()

這是崩潰報告的截圖,爲什麼在線0

enter image description here

這是其中的HTTPRequest的代碼從服務器獲取

func loadMetadata() { 

    if self.hasConnectivity() { 

     let url = NSURL(string: "http://sites.exampleserver.de/files/playlist/streamdaten_json.txt") 
     let request = NSMutableURLRequest(URL:url!) 

     let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() 
     sessionConfiguration.requestCachePolicy = .ReloadIgnoringLocalCacheData 
     let session = NSURLSession(configuration: sessionConfiguration, delegate: nil, delegateQueue: nil) 

     session.dataTaskWithRequest(request) { (data, response, error) -> Void in 

      let httpResponse = response as! NSHTTPURLResponse 
      let statusCode = httpResponse.statusCode 

      if (statusCode == 200) { 
       do{ 
        let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? NSDictionary 
        print(json) 
        var title : String 
        var interpret : String 
        switch (self.channels.channel) { 
        case 0: 
         title = json?.objectForKey("Titel_Live") as! String 
         interpret = json?.objectForKey("Interpret_Live") as! String 
        case 1: 
         title = json?.objectForKey("Titel_InTheMix") as! String 
         interpret = json?.objectForKey("Interpret_InTheMix") as! String 

        default: 
         title = "" 
         interpret = "" 
        } 

        if self.lastTitle != title || self.lastInterpret != interpret || self.lastChannel != self.channels.channel { 

         if title.lengthWithoutWhitespace() < 1 && interpret.lengthWithoutWhitespace() < 1 { 
          self.songTitle = self.channels.currentDefaultArtist 
          self.albumTitle = self.channels.currentDefaultTitle 

         } else { 

          if self.lastTitle != title { // title has changed 
           self.albumTitle = title.lengthWithoutWhitespace() > 1 ? title : self.channels.currentDefaultArtist 
          } 
          if self.lastInterpret != interpret || self.lastChannel != self.channels.channel { // interpret has changed 
           self.songTitle = interpret.lengthWithoutWhitespace() > 1 ? interpret : self.channels.currentDefaultArtist 
          } 

         } 
        } 

        self.lastTitle = title 
        self.lastInterpret = interpret 
        self.lastChannel = self.channels.channel 

        // Test currentInfo 
        self.metaDataHandler.setInfo(artist: self.songTitle, title: self.albumTitle) 

       }catch { 
        print("Error with Json: \(error)") 
       } 
      } 
      }.resume() 
    } 
} 

回答

1

不相關的崩潰JSON數據,但閱讀文檔,然後找出爲什麼使用.AllowFragment是絕對的胡說八道。

在斯威夫特,一個感嘆號的意思是:「我可以百分之百的肯定,這事不可能是零,而如果它是零,那麼請崩潰」。顯然,編譯器會不時地遵循你的指令和崩潰。

您的應用程序可能崩潰的第一個障礙是「響應」參數,它是可選。如果它是零,你的應用程序將崩潰,因爲這是你所要求的。毫不奇怪,它在各地崩潰。學習如何處理可選項並相應地更改您的代碼。

+0

感謝了很多!我已經瞭解了關於選擇,現在明白了問題的出處。 – shilei365