PerfURLConnectionWithDelegateClassInit.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2020 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. // Non-google3 relative import to support building with Xcode.
  15. #import "PerfURLConnectionWithDelegateClassInit.h"
  16. #import "PerfNetworkConnection+Protected.h"
  17. @interface PerfURLConnectionWithDelegateClassInit ()
  18. @property(nonatomic, copy) NSString *urlString;
  19. @property(nonatomic, copy) SuccessNetworkCallback successCallback;
  20. @property(nonatomic, copy) FailureNetworkCallback failureCallback;
  21. @property(nonatomic, strong) NSURLConnection *URLConnection;
  22. @end
  23. @implementation PerfURLConnectionWithDelegateClassInit
  24. #pragma mark - NetworkConnection
  25. - (void)makeNetworkRequestWithSuccessCallback:(SuccessNetworkCallback)success
  26. failureCallback:(FailureNetworkCallback)fail {
  27. [self logOperationStart];
  28. self.successCallback = success;
  29. self.failureCallback = fail;
  30. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.urlString]];
  31. self.URLConnection = [NSURLConnection connectionWithRequest:request delegate:self];
  32. [self.URLConnection start];
  33. }
  34. #pragma mark - NSURLConnectionDelegate methods
  35. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  36. [self logOperationFail];
  37. self.failureCallback(error);
  38. [self clearCallbacks];
  39. }
  40. #pragma mark - NSURLConnectionDataDelegate methods
  41. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  42. [self logOperationSuccess];
  43. self.successCallback();
  44. [self clearCallbacks];
  45. }
  46. #pragma mark - Private methods
  47. - (void)clearCallbacks {
  48. self.failureCallback = nil;
  49. self.successCallback = nil;
  50. }
  51. @end