I have a WebServiceCaller class that uses NSURLConnection for asynchronous calls to a web service. The class provides a delegate property and when the web service call is completed, it calls the webServiceDoneWi method, thanksXX on the deletion.
There are several web service methods, two of which are GetSummary and GetList.
In classes that use WebServiceCaller, you first need both a summary and a list so that they are written as follows:
-(void)getAllData {
[webServiceCaller getSummary];
}
-(void)webServiceDoneWithGetSummary {
[webServiceCaller getList];
}
-(void)webServiceDoneWithGetList {
...
}
This works, but there are at least two problems:
- Calls are shared between method delegates, so itโs hard to see the sequence at a glance, but itโs more important to control it or change the sequence.
- GetSummary, GetList,
,
webServiceDoneWithGetSummary
GetList .
, GetList , GetSummary , GetList.
- ?
, :
, , โ2, , (GetSummary + GetList) GetSummary. - GetSummary. GetSummaryDone ( - , , GetList).
-(void)getAllData {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getSummaryDoneAndCallGetList:)
name:kGetSummaryDidFinish object:nil];
[webServiceCaller getSummary];
}
-(void)getSummaryDoneAndCallGetList {
[NSNotificationCenter removeObserver]
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getListDone:)
name:kGetListDidFinish object:nil];
[webServiceCaller getList];
}
-(void)getListDone {
[NSNotificationCenter removeObserver]
}
-(void)getJustSummaryData {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getJustSummaryDone:)
name:kGetSummaryDidFinish object:nil];
[webServiceCaller getSummary];
}
-(void)getJustSummaryDone {
[NSNotificationCenter removeObserver]
}
. , if-then, . 1.