Search code examples
iosobjective-cuiactivityviewcontrollerdispatch-async

dispatch_async method implement second method without execute first method


NSString *firstID = @"https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";

imagine there my firstID is thumbnail image when i pass first id it goes to get full image and get original image

    dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"0");
                [self GetFullImage:firstID];
                NSLog(@"1");
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"2");
                    UIActivityViewController *activityViewController = [[UIActivityViewController alloc]                                                                initWithActivityItems:PAN applicationActivities:nil];
                    activityViewController.popoverPresentationController.sourceView = self.view;
                    [self presentViewController:activityViewController animated:YES completion:nil];
                    NSLog(@"3");
                    NSLog(@"PAGGGGGG:%@",PAN);

                });
            });

first method code:

- (void)GetFullImage:(NSString *)FullPath{
    NSString *UserName = [Appdata sharedDataModel].userNameString;
    NSString *Password = [Appdata sharedDataModel].passwordString;
    NSString *DeviceType = [Appdata device];
    NSString *VersionNo = [Appdata buildVersion];

    NSMutableDictionary *parameterDict = [[NSMutableDictionary alloc] init];

    [parameterDict setObject:UserName forKey:@"UserName"];
    [parameterDict setObject:Password forKey:@"Password"];
    [parameterDict setObject:DeviceType forKey:@"DeviceType"];
    [parameterDict setObject:VersionNo forKey:@"VersionNo"];
    [parameterDict setObject:FullPath forKey:@"FullPath"];

    [[APIUtility sharedInstance] get_Orig_Full_Image_Disp:parameterDict finishResult:^(id response) {

        if(response){
            @try {
                iconImage=nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SHaREDATA removeAllObjects];
                    [SHaREDATA addObject:response];
                    NSLog(@"15975368420:%@",SHaREDATA);

                    NSString *FullImage = [[[SHaREDATA objectAtIndex:0]valueForKey:@"Path"]objectAtIndex:0];
                    NSLog(@"ppppppppppp:%@",FullImage);
                    NSData* data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:FullImage]];
                    iconImage = [UIImage imageWithData:data];
                    NSLog(@"YYYYYYYYYY:%@",iconImage);
                    NSString  * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.jpg"];
                    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
                    PAN =@[iconImage];
                });

            }
            @catch (NSException *exception) {
            }
        }
    }];
}

results(i get):

2018-04-11 10:03:56.105 search[2119:29318] 0
2018-04-11 10:03:56.107 search[2119:29318] 1
2018-04-11 10:03:56.108 search[2119:29318] 2
2018-04-11 10:03:56.206 search[2119:29318] 3
2018-04-11 10:03:56.206 search[2119:29318] PAGGGGGG:(null)
2018-04-11 10:10:47.299 search[2208:32130] returnDictionary = (
    {
    Caption = "";
    Path = "https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";
    ReturnValue = 0;
    }
)
2018-04-11 10:10:47.299 search[2208:32130] 15975368420:(
    (
       {
        Caption = "";
        Path = "https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";
        ReturnValue = 0;
       }
    )
 )
 2018-04-11 10:10:47.300 search[2208:32130] 
 ppppppppppp:https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg
 2018-04-11 10:10:49.187 search[2208:32130] YYYYYYYYYY:<UIImage:0x7c815d70>, {960, 720}

i tried many ways but that all execute like my result i want to execute first get full image than it execute UIActivityController please help me i stuck in this many days.

when i use dispatch_sync it block method, which is inside this method.


Solution

  • In my opinion, you should use block in this case. Remake GetFullImage: method with a completionBlock and show UIActivityController in completionBlock. You can check the code below

    - (void)GetFullImage:(NSString *)FullPath completion:(void(^)(UIImage* image))completion{
      NSString *UserName = [Appdata sharedDataModel].userNameString;
      NSString *Password = [Appdata sharedDataModel].passwordString;
      NSString *DeviceType = [Appdata device];
      NSString *VersionNo = [Appdata buildVersion];
    
      NSMutableDictionary *parameterDict = [[NSMutableDictionary alloc] init];
    
      [parameterDict setObject:UserName forKey:@"UserName"];
      [parameterDict setObject:Password forKey:@"Password"];
      [parameterDict setObject:DeviceType forKey:@"DeviceType"];
      [parameterDict setObject:VersionNo forKey:@"VersionNo"];
      [parameterDict setObject:FullPath forKey:@"FullPath"];
    
      [[APIUtility sharedInstance] get_Orig_Full_Image_Disp:parameterDict finishResult:^(id response) {
    
        if(response){
          @try {
            iconImage=nil;
            dispatch_async(dispatch_get_main_queue(), ^{
              [SHaREDATA removeAllObjects];
              [SHaREDATA addObject:response];
              NSLog(@"15975368420:%@",SHaREDATA);
    
              NSString *FullImage = [[[SHaREDATA objectAtIndex:0]valueForKey:@"Path"]objectAtIndex:0];
              NSLog(@"ppppppppppp:%@",FullImage);
              NSData* data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:FullImage]];
              iconImage = [UIImage imageWithData:data];
              NSLog(@"YYYYYYYYYY:%@",iconImage);
              NSString  * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.jpg"];
              [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
              PAN =@[iconImage];
    
              if (completion) {
                completion(iconImage);
              }
            });
    
          }
          @catch (NSException *exception) {
            if (completion) {
              completion(nil);
            }
          }
        }
      }];
    }
    

    Usage:

    [self GetFullImage:firstID completion:^(UIImage *image) {
      if (!image) {
        return;
      }
    
      NSLog(@"2");
      UIActivityViewController *activityViewController = [[UIActivityViewController alloc]                                                                initWithActivityItems:@[iconImage] applicationActivities:nil];
      activityViewController.popoverPresentationController.sourceView = self.view;
      [self presentViewController:activityViewController animated:YES completion:nil];
      NSLog(@"3");
      NSLog(@"PAGGGGGG:%@",image);
    }];