Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performSelectorInBackground, notify other viewcontroller when done

I have a method used to save an image when the user clicks Save. I use performSelectorInBackground to save the image, the viewcontroller is popped and the previous viewcontroller is shown.

I want the table (on the previousUIViewController) to reload its data when the imagesaving is done.

How can I do this?

The save method is called like this:

[self performSelectorInBackground:@selector(saveImage) withObject:nil];
[self.navigationController popViewControllerAnimated:YES];
like image 560
McDJ Avatar asked Jan 20 '10 17:01

McDJ


2 Answers

In your saveImage method, post a notification just after finishing saving the image and before returning from the method. Something like this:

// post notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageSaved" object:nil];

In the controller handling the table, implement

- (void) imageSaved:(NSNotification *)notification{

    [self.tableView reloadData];

}

and in its viewDidLoad method add the following code to register for notifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                selector:@selector(imageSaved:)
                                                 name:@"ImageSaved" object:nil];

finally, unregister in the dealloc method adding

[[NSNotificationCenter defaultCenter] removeObserver:self];
like image 80
Massimo Cafaro Avatar answered Oct 04 '22 18:10

Massimo Cafaro


I think the way to go is calling the method at the end of the saveImage routine. Maybe something like

[self performSelectorInBackground:@selector(saveImage) withObject:previousView];

And if you want to keep saveImage agnostic, create a protocol with a callback that your previousView can use.

@protocol processingFinishedDelegate
-(void)processingFinished;
@end

so At the end of saveImage you'll have:

[(id<processingFinishedDelegate>)object processingFinished];

and of course your previousView class interface should handle the delegate.

like image 42
NWCoder Avatar answered Oct 04 '22 17:10

NWCoder