Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload parallelism and retries #46

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions Tree Tracker/Screens/Upload/UploadViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ final class UploadViewModel: CollectionViewModel {
var rightNavigationButtonsPublisher: Published<[NavigationBarButtonModel]>.Publisher { $rightNavigationButtons }
var dataPublisher: Published<[ListSection<CollectionListItem>]>.Publisher { $data }

let maxConcurrentUploads = 3
let maxUploadAttempts = 3
private var api: Api
private var database: Database
private var screenLockManager: ScreenLockManaging
private var logger: Logging
private var sites: [Site] = []
private var species: [Species] = []
private var supervisors: [Supervisor] = []
private var currentUpload: Cancellable?
private var currentUploads: [String: Cancellable?] = [:]
private var failedUploadCount: [String: Int] = [:]
private weak var navigation: UploadNavigating?

init(api: Api = CurrentEnvironment.api, database: Database = CurrentEnvironment.database, screenLockManager: ScreenLockManaging = CurrentEnvironment.screenLockManager, logger: Logging = CurrentEnvironment.logger, navigation: UploadNavigating) {
Expand Down Expand Up @@ -107,6 +110,8 @@ final class UploadViewModel: CollectionViewModel {
presentUploadButton(isUploading: true)
presentNavigationButtons(isUploading: true)
screenLockManager.disableLocking()
self.currentUploads = [:]
self.failedUploadCount = [:]
uploadLocalTreesRecursively()
}

Expand All @@ -125,44 +130,58 @@ final class UploadViewModel: CollectionViewModel {

private func cancelUploading() {
logger.log(.upload, "Uploading cancelled.")
currentUpload?.cancel()
for ((_, upload)) in currentUploads {
upload?.cancel();
}
stopUploading()
}

private func uploadLocalTreesRecursively() {
logger.log(.upload, "Uploading images...")
database.fetchLocalTrees { [weak self] trees in
self?.logger.log(.upload, "Trees to upload: \(trees.count)")

guard let tree = trees.sorted(by: \.createDate, order: .descending).first else {
self?.logger.log(.upload, "No more items to upload - bailing.")
self?.stopUploading()
return
}

self?.logger.log(.upload, "Now uploading tree: \(tree)")
self?.currentUpload = self?.api.upload(
tree: tree,
progress: { progress in
self?.logger.log(.upload, "Progress: \(progress)")
self?.update(uploadProgress: progress, for: tree)
},
completion: { result in
switch result {
case let .success(airtableTree):
self?.logger.log(.upload, "Successfully uploaded tree.")
self?.database.save([airtableTree], sentFromThisDevice: true)
self?.database.remove(tree: tree) {
self?.presentTreesFromDatabase()
let sortedTrees = trees.sorted(by: \.createDate, order: .descending)
while (self != nil && self!.currentUploads.count < self!.maxConcurrentUploads && self!.currentUploads.count < trees.count) {

guard let tree = sortedTrees.first(where: { treeEntry in self?.currentUploads[treeEntry.phImageId] == nil && (self?.failedUploadCount[treeEntry.phImageId] ?? 0) < self!.maxUploadAttempts}) else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌🏼

self?.logger.log(.upload, "No more items to upload - bailing.")
self?.stopUploading()
return
}

self?.logger.log(.upload, "Now uploading tree: \(tree)")
self?.currentUploads[tree.phImageId] = (self?.api.upload(
tree: tree,
progress: { progress in
self?.logger.log(.upload, "Progress: \(progress)")
self?.update(uploadProgress: progress, for: tree)
},
completion: { result in
switch result {
case let .success(airtableTree):
self?.logger.log(.upload, "Successfully uploaded tree.")
self?.database.save([airtableTree], sentFromThisDevice: true)
self?.database.remove(tree: tree) {
self?.currentUploads[tree.phImageId] = nil;
if (self != nil && self!.currentUploads.count == 0) {
self?.presentUploadButton(isUploading: false)
}
self?.presentTreesFromDatabase()
self?.uploadLocalTreesRecursively()
}
case let .failure(error):
self?.failedUploadCount[tree.phImageId] = (self?.failedUploadCount[tree.phImageId] ?? 0) + 1
self?.currentUploads[tree.phImageId] = nil;
self?.update(uploadProgress: 0.0, for: tree)
self?.logger.log(.upload, "Error when uploading a local tree: \(error)")
self?.uploadLocalTreesRecursively()
if (self != nil && self!.currentUploads.count == 0) {
self?.presentUploadButton(isUploading: false)
}
Comment on lines +159 to +180

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uploadLocalTreesRecursively is getting a little long. The completion callback seems like a good candidate for extraction. It seems to only use self and tree.

}
case let .failure(error):
self?.update(uploadProgress: 0.0, for: tree)
self?.presentUploadButton(isUploading: false)
self?.logger.log(.upload, "Error when uploading a local tree: \(error)")
}
}
)
))
}
}
}

Expand Down