CodeHunter
CodeHunter

Reputation: 11

ResignFirstResponder crash

I`m trying to down keyboard before do a NSURLResquest and loading show...

[self.txtComentario resignFirstResponder]; 

crashes de app...I have already tried to resignFirstResponder inside loadingThread() too

-(void) loadingThread {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    [self.myLoadingView setFrame:CGRectMake(0, 0, 320, 568)];
    [self.myLoadingImagem setFrame:CGRectMake(133, 250, 54, 9)];
    [appDelegate.window addSubview:self.myLoadingView];
    [appDelegate.window addSubview:self.myLoadingImagem];
    [self.myLoadingView setHidden:NO];
    [self animar];
}


-(IBAction)btnComentarClick:(id)sender {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[self.txtComentario resignFirstResponder];
    [NSThread detachNewThreadSelector:@selector(loadingThread) toTarget:self withObject:nil];


    NSString* comentarios = [kBaseURL stringByAppendingPathComponent:kComentarios];

    NSURL* url = [NSURL URLWithString:comentarios]; //1

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST"; //2

    //        NSDictionary *usuarioDict = [[NSDictionary alloc] initWithObjectsAndKeys:txtEmailCadastro.text, @"email", txtSenhaCadastro.text, @"senha", categorias, @"categorias", nil];


    NSMutableDictionary* jsonable = [NSMutableDictionary dictionary];
    safeSet(jsonable, @"idUsuario", appDelegate.usuarioLogado.identificador);
    safeSet(jsonable, @"nomeUsuario", appDelegate.usuarioLogado.nome);
    safeSet(jsonable, @"textoComentario", self.txtComentario.text);
    safeSet(jsonable, @"idOcorrencia", _location._id);


    NSData* data = [NSJSONSerialization dataWithJSONObject:jsonable options:0 error:NULL]; //3

    request.HTTPBody = data;
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //4

    NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession* session = [NSURLSession sessionWithConfiguration:config];

    NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //5
        if (!error) {
//            NSArray* responseArray = @[[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]];
            NSLog(@"comentado");
            dispatch_async(dispatch_get_main_queue(), ^(void){
                    self.txtComentario.text = @"";
                [self.txtComentario resignFirstResponder];
            });
        }
    }];
    [dataTask resume];
    [self listarComentarios];
}

EDIT: if I try to resignFirstesponder before NSThread, nothing happens, no crash but not keyboard Down..If i try inside loadingThread...the app crashes

the error inside NSThread is:

[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

Upvotes: 0

Views: 427

Answers (2)

Bhavin
Bhavin

Reputation: 27225

It happens sometimes that asynchronous API requests won't call back to the delegate on the main thread. So, just try to ensure that you are on Main thread. If you aren't on it then try to switch to the main thread before you make any update to the UI.

if ([NSThread isMainThread]) {
    NSLog(@"Yes, it is!");
} else {
    NSLog(@"No, it's not. Please switch to main");
}

Upvotes: 1

Adir Ido
Adir Ido

Reputation: 1

try to replace with this code

[self.view endEditing: YES];

Upvotes: 0

Related Questions