我正在将数据从我的Swift应用程序上传到Amazon S3,它就像其他任何东西一样耗尽电池。如何避免这种情况?

时间:2022-11-26 23:03:24

In my 'Swift' app I have a feature of uploading photos to my Amazon S3 bucket. When the user is connected to WiFi or LTE, there's no problem, but when the connection is a little slower (e.g. 3G), then the upload takes a lot of time (up to one minute) and iphone can lose 15-20% of battery! I resize photos down to around 200-300kb, so that should not be a problem. The code that I use for that is:

在我的'Swift'应用程序中,我有一个将照片上传到我的Amazon S3存储桶的功能。当用户连接到WiFi或LTE时,没有问题,但是当连接速度稍慢(例如3G)时,上传需要花费很多时间(最多一分钟),iphone可能会损失15-20%电池!我将照片调整到大约200-300kb,这应该不是问题。我用的代码是:

func awsS3PhotoUploader(_ ext: String, pathToFile: String, contentType: String, automaticUpload: Bool){

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType,
                                                                identityPoolId:CognitoIdentityPoolId)
        let configuration = AWSServiceConfiguration(region:CognitoRegionType, credentialsProvider:credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration

        let uploadRequest = AWSS3TransferManagerUploadRequest()
        uploadRequest?.body = URL(string: "file://"+pathToFile)
        uploadRequest?.key = ProcessInfo.processInfo.globallyUniqueString + "." + ext
        uploadRequest?.bucket = S3BucketName
        uploadRequest?.contentType = contentType + ext

        uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
            DispatchQueue.main.async(execute: { () -> Void in
                if totalBytesExpectedToSend > 1 {
                    print(totalBytesSent)
                    print(totalBytesExpectedToSend)
                }
            })
        }
        let transferManager = AWSS3TransferManager.default()
        transferManager?.upload(uploadRequest).continue({ (task) -> AnyObject! in

            if (task.isCompleted) {
                  print("task completed")
            }

            if let error = task.error {
                 print("Upload failed ❌ (\(error))")

            }
            if let exception = task.exception {
                 print("Upload failed ❌ (\(exception))")

            }
            if task.result != nil {
                let s3URL: String = "https://myAlias.cloudfront.net/\((uploadRequest?.key!)!)"
                print("Uploaded to:\n\(s3URL)")
            }
            else {
                print("Unexpected empty result.")
            }
            return nil
        }
        )

}

Is there anything that comes up to your mind of what am I doing wrong here and how could this huge battery consumption be avoided?

有什么事情让你想到我在这里做错了什么?如何避免这种巨大的电池消耗?

1 个解决方案

#1


5  

The following answer was inspired by https://*.com/a/20690088/3549695

以下答案的灵感来自https://*.com/a/20690088/3549695

I believed what you need to do is the ability to detect the Type of the Radio Network. Be it WiFi, LTE, 3G, 2G, or No Network. Then the app will need to make the decision, based on the result.

我相信你需要做的是能够检测无线电网络的类型。无论是WiFi,LTE,3G,2G还是无网络。然后,应用程序将需要根据结果做出决定。

I created a test Xcode project to test this concept on my iPhone 6. It seems to work, but I could only test 'Air Plane Mode', WiFi and LTE. I can't get myself into 2G or 3G network.

我创建了一个测试Xcode项目来测试我的iPhone 6上的这个概念。它似乎工作,但我只能测试'空中飞机模式',WiFi和LTE。我不能让自己进入2G或3G网络。

In case of either WiFi or LTE, I will get the value: 'CTRadioAccessTechnologyLTE'

如果是WiFi或LTE,我将获得价值:'CTRadioAccessTechnologyLTE'

While in 'Air Plane Mode', the Optional value will be nil. So it's up to me what text I replace it with. And I choose to output 'Not able to detect'

在“Air Plane Mode”中,Optional值为零。因此,由我来取代它的文本取决于我。我选择输出'无法检测'

Here is what my ViewController.swift looks like:

这是我的ViewController.swift的样子:

import UIKit
import CoreTelephony

class ViewController: UIViewController {

    @IBOutlet weak var currentRAN: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func detect(_ sender: UIButton) {
        if case let telephonyInfo = CTTelephonyNetworkInfo(),
            let currentRadioAccessTech = telephonyInfo.currentRadioAccessTechnology {

            currentRAN.text = currentRadioAccessTech
            print("Current Radio Access Technology: \(currentRadioAccessTech)")

        } else {
            currentRAN.text = "Not able to detect"
            print("Not able to detect")
        }
    }
}

Where the possible values for .currentRadioAccessTechnology are:

.currentRadioAccessTechnology的可能值为:

/*
* Radio Access Technology values
*/
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyGPRS: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyEdge: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyWCDMA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyHSDPA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyHSUPA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMA1x: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORev0: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORevA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORevB: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyeHRPD: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyLTE: String

#1


5  

The following answer was inspired by https://*.com/a/20690088/3549695

以下答案的灵感来自https://*.com/a/20690088/3549695

I believed what you need to do is the ability to detect the Type of the Radio Network. Be it WiFi, LTE, 3G, 2G, or No Network. Then the app will need to make the decision, based on the result.

我相信你需要做的是能够检测无线电网络的类型。无论是WiFi,LTE,3G,2G还是无网络。然后,应用程序将需要根据结果做出决定。

I created a test Xcode project to test this concept on my iPhone 6. It seems to work, but I could only test 'Air Plane Mode', WiFi and LTE. I can't get myself into 2G or 3G network.

我创建了一个测试Xcode项目来测试我的iPhone 6上的这个概念。它似乎工作,但我只能测试'空中飞机模式',WiFi和LTE。我不能让自己进入2G或3G网络。

In case of either WiFi or LTE, I will get the value: 'CTRadioAccessTechnologyLTE'

如果是WiFi或LTE,我将获得价值:'CTRadioAccessTechnologyLTE'

While in 'Air Plane Mode', the Optional value will be nil. So it's up to me what text I replace it with. And I choose to output 'Not able to detect'

在“Air Plane Mode”中,Optional值为零。因此,由我来取代它的文本取决于我。我选择输出'无法检测'

Here is what my ViewController.swift looks like:

这是我的ViewController.swift的样子:

import UIKit
import CoreTelephony

class ViewController: UIViewController {

    @IBOutlet weak var currentRAN: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func detect(_ sender: UIButton) {
        if case let telephonyInfo = CTTelephonyNetworkInfo(),
            let currentRadioAccessTech = telephonyInfo.currentRadioAccessTechnology {

            currentRAN.text = currentRadioAccessTech
            print("Current Radio Access Technology: \(currentRadioAccessTech)")

        } else {
            currentRAN.text = "Not able to detect"
            print("Not able to detect")
        }
    }
}

Where the possible values for .currentRadioAccessTechnology are:

.currentRadioAccessTechnology的可能值为:

/*
* Radio Access Technology values
*/
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyGPRS: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyEdge: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyWCDMA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyHSDPA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyHSUPA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMA1x: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORev0: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORevA: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyCDMAEVDORevB: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyeHRPD: String
@available(iOS 7.0, *)
public let CTRadioAccessTechnologyLTE: String