swift - AWS S3上传图像然后崩溃

时间:2017-02-09 20:06:16

标签: amazon-web-services amazon-s3 swift3

我正在使用以下代码上传数据(图片),所以亚马逊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

1 个答案:

答案 0 :(得分:0)

大。我只需要将控制器推到主线程上的导航控制器......很棒。再见2小时

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