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 "FIRStorageDeleteTask.h"
  15. #import "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. #pragma clang diagnostic push
  51. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  52. strongSelf->_fetcherCompletion = ^(NSData *_Nullable data, NSError *_Nullable error) {
  53. if (!self.error) {
  54. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  55. }
  56. if (callback) {
  57. callback(self.error);
  58. }
  59. self->_fetcherCompletion = nil;
  60. };
  61. #pragma clang diangostic pop
  62. [fetcher beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  63. weakSelf.fetcherCompletion(data, error);
  64. }];
  65. }];
  66. }
  67. @end