Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. -(NSArray)some function {
  2. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
  3. success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  4. NSArray *jsonArray =[JSON valueForKey:@"posts"];
  5. }
  6. failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
  7. }
  8.  
  9. - (void)goFetch:(id)caller
  10. {
  11. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
  12. success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  13.  
  14. [caller takeThisArrayAndShoveIt:[JSON valueForKey:@"posts"]];
  15. }
  16. failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
  17. }
  18.  
  19. - (void)goFetch:(void(^)(NSArray *))completion
  20. {
  21. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
  22. success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  23. if( completion ) completion([JSON valueForKey:@"posts"]);
  24. }
  25. failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
  26. }
  27.  
  28. typedef void (^Completion)(NSArray* array, NSError *error);
  29.  
  30. -(void)someFunctionWithBlock:(Completion)block {
  31. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
  32. success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  33. NSArray *jsonArray =[JSON valueForKey:@"posts"];
  34. if (block) block(jsonArray, nil);
  35. }
  36. failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
  37. if (block) block(nil, error);
  38. }
  39. }
  40.  
  41. [yourClassInstance someFunctionWithBlock:^(NSArray* array, NSError *error) {
  42. if (error) {
  43. NSLog(%@"Oops error: %@",error.localizedDescription);
  44. } else {
  45. //do what you want with the returned array here.
  46. }
  47. }];
  48.  
  49. +(void)request:(NSString *)link parameters:(NSDictionary *)params forInstance:(id)instance returns:(SEL)returnValue
  50. {
  51. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  52. [manager GET:link
  53. parameters:params
  54. success:^(AFHTTPRequestOperation *operation, id responseObject)
  55. {
  56. [instance performSelector:returnValue withObject: responseObject];
  57. }
  58. failure:^(AFHTTPRequestOperation *operation, NSError *error)
  59. {
  60. [instance performSelector:returnValue withObject:nil];
  61. //NSLog(@"Error: %@", error);
  62. }];
  63. }
  64.  
  65. - (RXCM_TroubleTypes) logic_getEnumValueOfCurrentCacheProblem
  66. {
  67. RXCM_TroubleTypes result = RXCM_HaveNotTrouble;
  68. NetworkStatus statusConnection = [self network_typeOfInternetConnection];
  69.  
  70. RXCM_TypesOfInternetConnection convertedNetStatus = [RXCM convertNetworkStatusTo_TypeOfInternetConnection:statusConnection];
  71.  
  72.  
  73. BOOL isAllowed = [self someMethodWith:convertedNetStatus];
  74. if (isAllowed){
  75. return RXCM_HaveNotTrouble;
  76. }else {
  77. return RXCM_Trouble_NotSuitableTypeOfInternetConnection;
  78. }
  79.  
  80. return result;
  81. }
  82.  
  83. - (BOOL) isUserPermissioned:(RXCM_TypesOfInternetConnection)newType
  84. {
  85. __block BOOL isReceivedValueFromBlock = NO;
  86. __block BOOL result = NO;
  87. __block BOOL isCalledDelegateMethod = NO;
  88.  
  89. dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  90. dispatch_sync(aQueue,^{
  91.  
  92. while (!isReceivedValueFromBlock) {
  93. NSLog(@"While");
  94. if (!isCalledDelegateMethod){
  95. [self.delegate rxcm_isAllowToContinueDownloadingOnNewTypeOfInternetConnection:newType
  96. completion:^(BOOL isContinueWorkOnNewTypeOfConnection) {
  97. result = isContinueWorkOnNewTypeOfConnection;
  98. isReceivedValueFromBlock = YES;
  99. }];
  100. isCalledDelegateMethod = YES;
  101. }
  102. [NSThread sleepForTimeInterval:0.5];
  103.  
  104. }
  105. });
  106. return result;
  107. }
  108.  
  109. - (void) rxcm_isAllowToContinueDownloadingOnNewTypeOfInternetConnection:(RXCM_TypesOfInternetConnection)newType
  110. completion:(void(^)(BOOL isContinueWorkOnNewTypeOfConnection))completion
  111. {
  112. __weak ViewController* weak = self;
  113.  
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert"
  116. message:@"to continue download on the new type of connection"
  117. preferredStyle:UIAlertControllerStyleAlert];
  118.  
  119. UIAlertAction *ok = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  120. completion(YES);
  121. }];
  122.  
  123. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  124. completion(NO);
  125. }];
  126.  
  127. [alert addAction:cancel];
  128. [alert addAction:ok];
  129. [weak presentViewController:alert animated:YES completion:nil];
  130.  
  131. });
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement