When using delegates, you need a better way for sequential processing

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]
    //process summary data

    [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(getListDone:) 
                 name:kGetListDidFinish object:nil];
    [webServiceCaller getList];
}
-(void)getListDone {
    [NSNotificationCenter removeObserver]
    //process list data 
}


-(void)getJustSummaryData {
    [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(getJustSummaryDone:)     //different selector but
                 name:kGetSummaryDidFinish object:nil];  //same notification name
    [webServiceCaller getSummary];
}
-(void)getJustSummaryDone {
    [NSNotificationCenter removeObserver]
    //process summary data
}

. , if-then, . 1.

+3
1

, - ( ) , , . - , , . , -connectionDidFinishLoading ( webServiceCaller, ), , , . - :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [connection release];
    // receivedData is an NSMutableData object we've been appending
    // to in the -didReceiveData delegate. We'll pass it in the
    // notification so we can hand the data off to the next request.
    [[NSNotificationCenter defaultCenter] 
           postNotificationName:kGetSummaryDidFinish object:receivedData];
}

, , :

- (void)viewDidLoad;
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(getSummaryDidFinish:) 
                 name:kGetSummaryDidFinish object:nil];
}

- (void) getSummaryDidFinish:(NSNotification*)notification;
{
    // If you needed the downloaded data, we passed it in
    NSData *data = [notification object];

    // Decide here if you want to call getList or not.
    if (someConditionOfDataObjectIsTrue)
        [webServiceCaller getList];
}

, , , , , ( , ). . , , , .

, .

+2

Source: https://habr.com/ru/post/1745332/


All Articles