0

I used below code for calling image posting first and then text posting upon completion of image posting method...

-(IBAction)btnChooseClecked:(id)sender
{

                NSOperationQueue *queue = [NSOperationQueue new];

                queue.maxConcurrentOperationCount = 1;

                [queue  addOperationWithBlock:^{
                    [self sendImage];
                }];

                [queue  addOperationWithBlock:^{
                    [NSThread sleepForTimeInterval:1.0];//2.0
                    [self sendText];
                }];

    ///(OR) i used below code also

    dispatch_async(dispatch_get_main_queue(), ^{


                [self sendImage]; 

    // inside sendImage method I am calling sendText as [self performSelector:@selector(sendText) withObject:nil afterDelay:0.2];


            })

    }

-(void)sendImage

 {
        NSData *imageData = UIImageJPEGRepresentation([appDelegate scaleAndRotateImage:imageSelected.image], 0.0);

    [appDelegate.hub invoke:@"Send" withArgs:@[([imageData respondsToSelector:@selector(base64EncodedStringWithOptions:)] ? [imageData base64EncodedStringWithOptions:kNilOptions] : [imageData base64Encoding])]];

        [self performSelector:@selector(sendText) withObject:nil afterDelay:0.5];
 }

But sometimes it is working fine but sometimes text is posting first instead of image. No delegate method is called after completion of first..As we used different API for performing post method. Please suggest any ideas where I am going wrong..Any alternatives for above..

Thanks in Advance..

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
  • Use Block concept of Objective- C. It will surly comes with output what you want. – Arpit Kulsreshtha Aug 28 '14 at 07:30
  • I have used block concept using dispatch as above..!@Arpit Kumar Kulshrestha – Vidhyanand Aug 28 '14 at 07:32
  • The way you are using is not interdependent ,I mean to say when sendImage completes with completionhandler then call sendText. Not the way you are using – Arpit Kulsreshtha Aug 28 '14 at 07:36
  • Have you considered using libraries like AFNetworking? Makes life rather easy to use these libraries. If you decide to use AFNetworking, you could probably call your sendText function in the success block of the request call. – Veeru Aug 28 '14 at 07:47
  • can you show the code of [self sendImage]. the response of [self sendImage] coming wher? – BHASKAR Aug 28 '14 at 07:57
  • @veeru,@BHASKAR I updated my question plz refer it. I am using separate API for post method.. – Vidhyanand Aug 28 '14 at 08:24
  • @Vidyanand, using sleep technique and assuming it works as expected is not totally right. What if the sendimage takes more than 1 second, sendtext is going to be called first. You have to change your code to call sendtext only if sendimage has completed, not by assuming it will be done in one second. – Veeru Aug 28 '14 at 08:49

0 Answers0