StorageDeleteTask.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. #if COCOAPODS
  16. import GTMSessionFetcher
  17. #else
  18. import GTMSessionFetcherCore
  19. #endif
  20. /**
  21. * Task which provides the ability to delete an object in Firebase Storage.
  22. */
  23. internal class StorageDeleteTask: StorageTask, StorageTaskManagement {
  24. private var fetcher: GTMSessionFetcher?
  25. private var fetcherCompletion: ((Data?, NSError?) -> Void)?
  26. private var taskCompletion: ((_ error: Error?) -> Void)?
  27. internal init(reference: StorageReference,
  28. fetcherService: GTMSessionFetcherService,
  29. queue: DispatchQueue,
  30. completion: ((_: Error?) -> Void)?) {
  31. super.init(reference: reference, service: fetcherService, queue: queue)
  32. taskCompletion = completion
  33. }
  34. deinit {
  35. self.fetcher?.stopFetching()
  36. }
  37. /**
  38. * Prepares a task and begins execution.
  39. */
  40. internal func enqueue() {
  41. weak var weakSelf = self
  42. dispatchQueue.async {
  43. guard let strongSelf = weakSelf else { return }
  44. strongSelf.state = .queueing
  45. var request = strongSelf.baseRequest
  46. request.httpMethod = "DELETE"
  47. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime
  48. let callback = strongSelf.taskCompletion
  49. strongSelf.taskCompletion = nil
  50. let fetcher = strongSelf.fetcherService.fetcher(with: request)
  51. fetcher.comment = "DeleteTask"
  52. strongSelf.fetcher = fetcher
  53. strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
  54. if let error = error, self.error == nil {
  55. self.error = StorageErrorCode.error(withServerError: error, ref: strongSelf.reference)
  56. }
  57. callback?(self.error)
  58. self.fetcherCompletion = nil
  59. }
  60. strongSelf.fetcher?.beginFetch { data, error in
  61. let strongSelf = weakSelf
  62. if let fetcherCompletion = strongSelf?.fetcherCompletion {
  63. fetcherCompletion(data, error as? NSError)
  64. }
  65. }
  66. }
  67. }
  68. }