Поиск по тэгу «alamofire»

Downloading files using Alamofire in OperationQueue

Operation object might be deallocated while Alamofire performing async download of a file inside. So if you need to do something that depends of your Operation class property you can override isFinished property to wait while callback will be called:

class TestOperation: Operation {
  private var downloaded: Bool = false
  let urlString: String
  override var isFinished: Bool {
    get {
      return downloaded
  // MARK: - Init
  init(urlString: String) {
    self.urlString = urlString
  override func main() {
    if self.isCancelled {return }
  override func start() {
    if self.isCancelled { return }
    print("Starting downloading from url: \(urlString)")
    let destination = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0], to: destination)
      .response { [unowned self] response in
        defer {
          // operation will be deallocated after setting true
          self.downloaded = true
        if self.isCancelled { return }
        print("Finished downloading from url: \(self.urlString)")
// create queue 
var downloadQueue: OperationQueue = {
  var queue = OperationQueue() = "Download queue"
  return queue
// create operation and add to queue
let op = TestOperation(urlString: "done")


comment comments