0

I am using the thread to call my function "initialGetMethod"

[NSThread detachNewThreadSelector:@selector(initialGetMethod) toTarget:self withObject:nil];

and my get method is

-(void) initialGetMethod
{
    self.loginPassword = [[ UIAlertView alloc] initWithTitle:@"Please Login to MFP" message:@"Enter Valid UserID and Password" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    [self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    [self.loginPassword setTag:2];
    [self.loginPassword show];
}

but its giving the exception "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. "

its giving the exception at "[self.loginPassword setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];"

and if i call the function as "[self initialGetMethod];" its not giving the exception but it will take some time..

i tried loading in background but its not working.. (mean i dont want it to be in background)..

please suggest some solution ..

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Raju
  • 39
  • 6

2 Answers2

2

The error which you are getting during running application is

"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. "

which is occur once you are updating or accessing the UI elements in any other thread except the MainThread (Use Main thread only to access or Update the UI it will only help you)

Here you are showing Alert in Background thread that's why it happens

Please use one of the following to PopUp alert

  [self performSelector:@selector(initialGetMethod) withObject:nil];

  [self performSelector:@selector(initialGetMethod) withObject:nil afterDelay:0.1];
iDhaval
  • 7,824
  • 2
  • 21
  • 30
-1
 //call createthread:

    [self setupTimerThread:];



       //write following code in setupTimerThread
       timer = [NSTimer timerWithTimeInterval:02
                                        target:self
                                      selector:@selector(initialGetMethod:)
                                      userInfo:nil
                                       repeats:NO];
       NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
       [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
       [runLoop run];
Anil
  • 1,028
  • 9
  • 20
  • Try my edited answer. It should work if problem is due to UI update in background. – Anil Apr 17 '13 at 07:11
  • Try my edited answer. May be it would work if only runloop changes, keeping the thread same. – Anil Apr 17 '13 at 07:26
  • You can't run UI operations on a background thread only the main thread to do UI operations. This is why your code doesn't work you are creating a new thread and trying to run the UI operations on it. This is not correct. – Popeye Apr 17 '13 at 07:30