Trying to upload Multiple files to AWS using AWSS3TransferUtility in the Background

93 views Asked by At

I have a situation in my App which I need to upload several files ranging from 10-50+- (small size, about 5 mb) and also need to support continuing in the background if needed. At this point I'm able to upload perfectly while the app is in the foreground but once the app goes into background the current running upload continue running and finish in the background, but for some reason the rest of the operations never gets called.

Here is my code

Background queue init:

    private var _backgroundUploadQueue = OperationQueue()

Background queue setup:

    self._backgroundUploadQueue.maxConcurrentOperationCount = 4
    self._backgroundUploadQueue.name = "Background Queue"
    self._backgroundUploadQueue.qualityOfService = .background

Creating the operetions:

private func _createUploadOperation(from action: S3UploadAction) -> AsyncBlockOperation {
    let operation = AsyncBlockOperation({ [weak self] operation in
        guard !operation.isCancelled else {
            operation.finish()
            return
        }
        self?._s3SinglePartUploader.upload(
            action: action,
            completion: { [weak self] result in
                self?._handleUploadCompletion(operation: operation, action: action, result: result)
            }
        )
        self?._singlePartUploadStarted(action: action)
    })
    operation.name = "\(action.recordingSetVersion)_\(action.partNumber)"

    return operation
}

Adding operations:

private func _startBackgroundUploading() {
    for (_, actions) in self._uploadActions.enumerated() {
        for action in actions.value {
            let uploadOperation = self._createUploadOperation(from: action)
            self._backgroundUploadQueue.addOperation(uploadOperation)
        }
    }
}

In the AppDelegate I add this code:

func application(
    _ application: UIApplication,
    handleEventsForBackgroundURLSession identifier: String,
    completionHandler: @escaping () -> Void
) {
    S3Uploader.shared.setBackgourndSessionCompletion(backgroundTaskCompletion: completionHandler)
    AWSS3TransferUtility.interceptApplication(
        application,
        handleEventsForBackgroundURLSession: identifier,
        completionHandler: completionHandler
    )
}
0

There are 0 answers