FIRStorageDeleteTask.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 Google
  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 "FirebaseStorage/Sources/FIRStorageDeleteTask.h"
  15. #import "FirebaseStorage/Sources/FIRStorageTask_Private.h"
  16. @implementation FIRStorageDeleteTask {
  17. @private
  18. FIRStorageVoidError _completion;
  19. }
  20. @synthesize fetcher = _fetcher;
  21. @synthesize fetcherCompletion = _fetcherCompletion;
  22. - (void)dealloc {
  23. [_fetcher stopFetching];
  24. }
  25. - (instancetype)initWithReference:(FIRStorageReference *)reference
  26. fetcherService:(GTMSessionFetcherService *)service
  27. dispatchQueue:(dispatch_queue_t)queue
  28. completion:(FIRStorageVoidError)completion {
  29. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  30. if (self) {
  31. _completion = [completion copy];
  32. }
  33. return self;
  34. }
  35. - (void)enqueue {
  36. __weak FIRStorageDeleteTask *weakSelf = self;
  37. [self dispatchAsync:^() {
  38. FIRStorageDeleteTask *strongSelf = weakSelf;
  39. if (!strongSelf) {
  40. return;
  41. }
  42. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  43. request.HTTPMethod = @"DELETE";
  44. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  45. FIRStorageVoidError callback = strongSelf->_completion;
  46. strongSelf->_completion = nil;
  47. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  48. strongSelf->_fetcher = fetcher;
  49. fetcher.comment = @"DeleteTask";
  50. strongSelf->_fetcherCompletion = ^(NSData *_Nullable data, NSError *_Nullable error) {
  51. if (!self.error) {
  52. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  53. }
  54. if (callback) {
  55. callback(self.error);
  56. }
  57. self->_fetcherCompletion = nil;
  58. };
  59. [fetcher beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  60. FIRStorageDeleteTask *strongSelf = weakSelf;
  61. if (strongSelf.fetcherCompletion) {
  62. strongSelf.fetcherCompletion(data, error);
  63. }
  64. }];
  65. }];
  66. }
  67. @end