2

I want to create a sort of queue where I can append work-items which can fail and retry or just succeed and are removed from the queue.

Basically what I am looking for a SomeQueue which should kind of work like:

let Task = (Data)->Result
    
let queue = SomeQueue(from: [task1, task2])
    .execute()
    .retry(3)
    .onFailure { failcount++ }
    
// Some other piece of code that kind of needs to work
Button(action: {
    queue.appendWorkItems([Task]())
})

Text(queue.remaining) // way to show unfinished items

I looked into OperationQueue, that kind of looked like it could work. I want it to be as modern and up to date swift as possible and I like the combine publish-subscribe stuff (it has to integrate with swiftUI). This looked like that was a better approach.

Do you have hints / ideas on how I could make this work?

Simon
  • 2,419
  • 2
  • 18
  • 30
  • 1
    Simon, if you want to do this in Combine, you’ll want to create a publisher for the upload task. Curiously, Apple never provided a Combine publisher for upload (or download) tasks, only data tasks. If you google "URLSession combine upload task" you’ll get hits like this: https://www.strv.com/blog/how-to-upload-files-using-combine-in-ios-track-their-progress-engineering. I once did a [download publisher](https://stackoverflow.com/a/32322851/1271826) and think the upload could easily follow the same pattern. Begs the question as to whether you should leapfrog Combine and go right to async-await. – Rob Nov 25 '21 at 02:02
  • @Rob I left 'upload' in the question sorry for that. The network stuff will ofc be async and I will manage to get that done. The problem is mostly with the task (completion/inprogress) management and how to add tasks to a sort of work-pool/queue, I hope that makes sense? I did find your first link, but not the second one, but it is all kind of about single api request or network requests and not really about queueing tasks in general. Also thanks for taking the time to respond :), e.g. how can I make that published, do somthing like append(task) and track how many published tasks are in queue. – Simon Nov 25 '21 at 09:30
  • I've written an upload publisher (modeled on Apple’s [`dataTaskPublisher`](https://github.com/apple/swift-corelibs-foundation/blob/eec4b26deee34edb7664ddd9c1222492a399d122/Darwin/Foundation-swiftoverlay/Publishers%2BURLSession.swift) (as well as one that creates the multipart request format). But the interesting question here, IMHO, is not the upload publisher, but rather how to create this “queue” concept. It really feels like there must be some obvious, existing, Combine type that does this for us, e.g. a `Publishers.Sequence` that lets us insert new publishers after the fact. Good question. – Rob Nov 26 '21 at 17:06

0 Answers0