S.J
S.J

Reputation: 3071

dispatch_async confliction

Currently i am working on a chatting app, I am trying to upload the image, every thing is working fine except that when image is uploading the UI freeze, so async approach came into the scene, this is what i am trying to do:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    [self dismissModalViewControllerAnimated:YES];
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
        dispatch_async(queue, ^{

        UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage]; 
        NSData *imgData = UIImageJPEGRepresentation(image, 1.0);

        //[self performSelectorOnMainThread:@selector(send:) withObject:imgData waitUntilDone:YES];

        [self send:imgData];
});

}

I am getting this error:

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

  1. WebThreadLock
  2. -[UITextView setText:]
  3. -[HPTextViewInternal setText:]
  4. -[HPGrowingTextView setText:]
  5. -[chatViewController send:]
  6. __74-[chatViewController imagePickerController:didFinishPickingMediaWithInfo:]_block_invoke_0
  7. _dispatch_call_block_and_release
  8. _dispatch_worker_thread2
  9. _pthread_wqthread
  10. start_wqthread

I am using HPGrowingTextView to give a iMessage kind of expandable typing area for typing messages, but getting this problem.

I searched this error

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread

peoples suggests using performSelectorOnMainThread but this approach again freeze the UI.

How to solve this conflict or is there any other approach.

Inside [self send:imageData]
...building a url and appending hFile(imageData)
[body appendData:[NSData dataWithData:hFile]];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            // setting the body of the post to the reqeust
            [request setHTTPBody:body];

            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

            NSString *imgUrl = [NSString stringWithFormat:@"http://www.www.www/uImages/thumbs/%@",returnString];
...

after uploading, thumbnail of image is returned, if I use [NSURLConnection sendAsynchronousRequest I get empty thumbnail which I am diplaying in uitableview.

Upvotes: 0

Views: 825

Answers (2)

Jody Hagins
Jody Hagins

Reputation: 28419

You are getting the crash because you are calling send outside the main thread. The stack trace is obvious about this fact.

You need to make those calls on the main thread. When you do, however, your UI will of course hang because of this call...

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

From the method name, it's obvious that this call is synchronous, and will block until the result is returned.

Thus, you need to use the asynchronous form instead.

sendAsynchronousRequest:queue:completionHandler:

Upvotes: 0

Hejazi
Hejazi

Reputation: 17015

When you want to change anything in the UI you should do it on the main thread.

So if you want to change the text of HPGrowingTextView control you have, you can do the following:

dispatch_async(dispatch_get_main_queue(), ^{
    growingTextView.text = @"Some text";
})

Upvotes: 1

Related Questions