Kio Coan
Kio Coan

Reputation: 581

Dissmiss uiview using PerformeSelectorOnMainThread

have a publish option in my app, where the user fills the information and it's send to my webservice.

I have the "form" on a modal view. And a function that I use to close it, when the button Cancel is touched.

When the publish button is touched, I start a thread, while the ws is in use.

I want to call my Cancel function when the ws operation is over.

Publish button:

 self.myThread = [[NSThread alloc]initWithTarget:self selector:@selector(startThread) object:nil];
            [self.myThread start];

startThread:

    [self.ws publicar:self.registro];
    [self.myThread performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];
    [self.myThread cancel];

Cancelar:

-(IBAction)Cancelar{

    [SingletonTelasAdicionar resetar];
    [self dismissViewControllerAnimated:YES completion:nil];
}

When I touch the publish button I get this:

2014-04-02 20:11:25.540 PetFinder[5356:60b] -[NSThread Cancelar]: unrecognized selector sent to instance 0x112100030 2014-04-02 20:11:25.541 PetFinder[5356:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSThread Cancelar]: unrecognized selector sent to instance 0x112100030' * First throw call stack: ( 0 CoreFoundation 0x0000000101b6f495 exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000010184799e objc_exception_throw + 43 2 CoreFoundation
0x0000000101c0065d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101b60d8d __forwarding
+ 973 4 CoreFoundation 0x0000000101b60938 _CF_forwarding_prep_0 + 120 5 Foundation
0x000000010144ca17 NSThreadPerformPerform + 227 6 CoreFoundation 0x0000000101afed21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 7 CoreFoundation 0x0000000101afe5f2 __CFRunLoopDoSources0 + 242 8 CoreFoundation 0x0000000101b1a46f __CFRunLoopRun + 767 9 CoreFoundation
0x0000000101b19d83 CFRunLoopRunSpecific + 467 10 GraphicsServices
0x0000000102cedf04 GSEventRunModal + 161 11 UIKit
0x00000001003f4e33 UIApplicationMain + 1010 12 PetFinder
0x0000000100008fc3 main + 115 13 libdyld.dylib
0x000000010381e5fd start + 1 14 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Upvotes: 1

Views: 110

Answers (2)

danh
danh

Reputation: 62686

The crash looks straight-forward: self.myThread is an NSThread. It doesn't implement Cancelar. The thing that does implement it is self. Try -

[self performSelectorOnMainThread:@selector(Cancelar) withObject:nil waitUntilDone:NO];

The design around it might also require some attention. If a view controller presents the form, how about doing this:

// on the vc that presents the 'form'
- (IBAction)submitAction:(id)sender {

    NSMutableURLRequest *request = // ... build a request to post your data
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        if (!error) {
            [self dismissViewControllerAnimated:YES completion:^{}];
        }
    }];
}

This runs the request off the main thread. When it's complete it runs the completion block on the main thread. There you dismiss the view controller that presented the form. Cancel would look like this:

- (IBAction)cancelAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

Upvotes: 2

user3491585
user3491585

Reputation: 11

You need the proper arguments for this kind of function.

Yours should be:

-(IBAction)Cancelar:(id)sender {
    [SingletonTelasAdicionar resetar];
    [self dismissViewControllerAnimated:YES completion:nil];
}

Also, you should change your selector call to:

[self.myThread performSelectorOnMainThread:@selector(Cancelar:) withObject:nil waitUntilDone:NO];

edit: formatting

Upvotes: 0

Related Questions