2017-02-09 65 views
0

我使用以下代碼來上傳數據(圖像),從而Amazon S3的迅速 - AWS S3上傳圖像,然後崩潰

func uploadData(data: Data, fileExtension: String, completion: @escaping (_ success: Bool, _ filename: String, _ url: String) ->()) { 

     if let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first { 

     let userId = 1 

     let fileName = "\(userId)_\(self.randomString()).\(fileExtension)" 

     let url = NSURL(fileURLWithPath: "\(documentsDirectoryPath)/\(fileName)") 
     let networkURLString = "\(appDel.assetAddress)/\(fileName)" 


     do { 

      try data.write(to: url as URL, options: .atomic) 

     } catch { 

      print("S3 Upload Error") 
      completion(false, "", "") 

     } 

     let uploadRequest: AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest() 
     uploadRequest.bucket = appDel.S3BucketName 
     uploadRequest.key = fileName 
     uploadRequest.body = url as URL! 

     let transferManager = AWSS3TransferManager.default() 
     let task = transferManager?.upload(uploadRequest) 

     task?.continue({ (task) -> Any? in 

      let success = (task.error == nil) && (task.result != nil) 

      if(!success){ 
       print("S3 Upload Error: \(task.error)") 
       completion(false, "", "") 
      } else { 
       completion(success, fileName, networkURLString) 
      } 

      return nil 
     }) 
    } else { 
     completion(false, "", "") 
    } 
} 

圖像被上傳成功;然而,在文件名和url字符串成功返回之後......在返回nil的行的後面,應用程序在後臺執行AWS清理需要很長時間,並且突然發生異常。

我試圖在完成時將控制器推到導航欄上,它會通過該代碼,但什麼都不做,因爲它在後臺執行任務......雖然圖像已上傳。

沒有什麼描述性的,當它崩潰,可以幫助我們找到問題..任何想法?

編輯:堆棧跟蹤

2017-02-09 15:37:06.453327 HighThere[2021:526754] *** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation/UIFoundation-491.4/UIFoundation/TextSystem/NSLayoutManager_Private.m:1577 
2017-02-09 15:37:06.462391 HighThere[2021:526754] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!' 
*** First throw call stack: 
(0x18d7e91b8 0x18c22055c 0x18d7e908c 0x18e2a1098 0x1935c33d4 0x1935c30b8 0x1935f2b90 0x1935f5ea8 0x19361a3a8 0x193619ab4 0x19374c8b8 0x19374c79c 0x1000e54d0 0x1000e0a98 0x1000e1390 0x1000e001c 0x1000e00f0 0x19369e924 0x1936b64b4 0x19383e5b4 0x193756e74 0x193756adc 0x193756a40 0x19369ba80 0x190b499d8 0x190b3e4cc 0x190b3e38c 0x190abb3e0 0x190ae2a68 0x190ae2f34 0x18c87bfbc 0x18c87bce4 0x18c87b378 0x18c87ad8c) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

回答

0

大。我只需要將控制器推到主線程上的導航控制器上......輝煌。再見2小時

DispatchQueue.main.async(execute: { 
        self.navigationController?.pushViewController(vcIntro(), animated: true) 
       })