2017-02-09 67 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) 
       })