Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect end of performSelectorInBackground:withObject:

I have an asynchronous server request in my iOS app:

[self performSelectorInBackground:@selector(doSomething) withObject:nil];

How can I detect the end of this operation?

like image 683
cuSoon Avatar asked Dec 05 '22 23:12

cuSoon


2 Answers

Put a call at the end of the doSomething method?!

- (void)doSomething {
    // Thread starts here

    // Do something

    // Thread ends here
    [self performSelectorOnMainThread:@selector(doSomethingDone) withObject:nil waitUntilDone:NO];
}
like image 164
Matthias Bauch Avatar answered Dec 07 '22 13:12

Matthias Bauch


If you just want to know when it's finished (and don't want to pass much data back - at which point I would recommend a delegate) you could simply post a notification to the notification center.

[[NSNotificationCenter defaultCenter] postNotificationName:kYourFinishedNotificationName object:nil];

within your view controllers viewDidLoad method add:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourNotificationListenerMethod:)
                                                 name:kYourFinishedNotificationName
                                               object:nil];

and within dealloc, add:

[[NSNotificationCenter defaultCenter] removeObserver:self];
like image 30
Nathan Jones Avatar answered Dec 07 '22 11:12

Nathan Jones