Reputation: 41
Hi I have an web service based app that will interchange data with our server. Since I have a dedicated class doing my work, the main view controller will actually call the worker every time. The worker itself knows when the connection finished since it is a NSURLConnectionDelegate. However I need to inform the main view controller whenever the work is done. The worker is a delegate of main view controller so it knows when it need to start working. Please help me.
Upvotes: 0
Views: 280
Reputation: 132
You can do it in two ways:
Approach #1:
User Local notifications. In the mainclass add an observer into LocalNotification Center in following way.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jobDone) name:@"WORKERJOBOVER" object:nil];
And in the worker class when the job is done, post nofitication to fire the selector:
[[NSNotificationCenter defaultCenter] postNotificationName:@"WORKERJOBOVER" object:nil];
Approach #2:
You can create a protocol of your worker class and add a method in the protocol which you can call on the delegate when your job is done in worker.
WorkerClass.h
//WorkerClass.h
@protocol WorkerDelegate
@interface WorkerClass: NSObject
@property (nonatomic, assign) id<WorkerDelegate> delegate
- (void)JobInProcess;
@end
@protocol WorkerDelegate
- (void)MyJobIsDone;
@end
WorkerClass.m
//WorkerClass.m
@implementation WorkerClass
@synthesize delegate = _delegate;
- (void)JobInProcess
{
//When job over this will fire callback method in main class
[self.delegate MyJobIsDone];
}
MainClass.h
//MainClass.h
#import WorkerClass.h
@interface MainClass: NSObject <WorkerDelegate>
@end
MainClass.m
//MainClass.m
@implementation MainClass
- (void)MyJobIsDone
{
//Do whatever you like
}
@end
Upvotes: 2
Reputation: 667
I would fire a notification from the background worker that you can listen for from your main view controller
In your View add the following when you want to listen.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(operationComplete:) name:@"operationComplete" object:nil];
In your background View notify that the event completed.
[[NSNotificationCenter defaultCenter] postNotificationName:@"operationComplete" object:nil];
Finally don't forget to remove the observer [[NSNotificationCenter defaultCenter] removeObserver:self];
Upvotes: 0
Reputation: 1484
You should do the other way around.
First declare your worker object as a member of your main view controller(and of course, make it a property), then from you main view controller, you can just call [self.worker sendRequstToServer]
to send the request.
Second, within your main view controller, wherever you initialize your work object, don't forget to put self = worker.delegate
.
Third, declare a delegate method in your worker class, something like -(void)webServiceCallFinished()
, and call [self.delegate webServiceCallFinished]
in your worker's -connectionDidFinishLoading()
(you might wanna do in -parserDidEndDocument()
if you are using NSXMLParer to parse xml)
Last, you wanna declare your main view controller as the delegate of your worker class and implement -webServiceCallFinished()
.
Hope this helps.
Upvotes: 2
Reputation: 6276
In viewDidLoad:
of your main view controller, set it as a notification observer with [NSNotificationCenter defaultCenter]
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webServiceContacted:) name:@"webServiceConnectionSuccessful" object:nil];
Then when your data was successfully interchanged with the server, post a notification within the network completion block:
[[NSNotificationCenter defaultCenter] postNotificationName:@"webServiceConnectionSuccessful" object:nil];
And webServiceContacted:
could look like this:
-(void)webServiceContacted:(NSNotification *)note
{
//do stuff here
}
Upvotes: 1