简体   繁体   English

在完成块完成后返回一个数组

[英]Return an array after a completion block has finished

I have the following function我有以下功能

- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    _currentStart = startDate;
    _currentEnd = lastDate;
    
    if(appDelegate.internetActive){
        Webservice *web = [[Webservice alloc]init];
        [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) {
            if(finished){
                [self generateRandomDataForStartDate:startDate endDate:lastDate];
                 // NOW return the self.dataArray
            }
        }];
    }
    return self.dataArray; 
}

I can't figure out how I can return the self.dataArray when the completionblock has finished.我不知道如何在completionblock返回self.dataArray Because my self.dataArray is filled inside the method generateRandomDataForStartDate:startDate .因为我的self.dataArray是在generateRandomDataForStartDate:startDate方法中填充的。 So at the moment the function always returns an empty array.所以目前该函数总是返回一个空数组。

You should pass the completion handler block inside the argument.您应该在参数中传递完成处理程序块。 Make the return type to void .使返回类型为void

Caller object will write below code:调用者对象将编写以下代码:

[calenderView calendarMonthView:monthView marksFromDate:startDate toDate:lastDate completionHandler:^(NSarray *dataArray){
//Process data array over here
}];

- (void) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate completionHandler:(void (^)(NSArray*))completionBlock{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    _currentStart = startDate;
    _currentEnd = lastDate;

    if(appDelegate.internetActive){
        Webservice *web = [[Webservice alloc]init];
        [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) {
            if(finished){
                [self generateRandomDataForStartDate:startDate endDate:lastDate];
                 completionBlock(self.dataArray);
            }
        }];
    }
    completionBlock(self.dataArray);
}

In the caller code handle completion block with response array received as argumnet.在调用者代码处理完成块中,响应数组作为参数接收。

You don't need to return an array from this method, As you mentioned that your dataArray is filling under the generateRandomDataForStartDate:startDate Method, So you can process the filled array from that method, So your code will be,您不需要从此方法返回数组,正如您提到的,您的 dataArray 正在generateRandomDataForStartDate:startDate方法下填充,因此您可以从该方法处理填充的数组,因此您的代码将是,

    - (void) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
   _currentStart = startDate;
   _currentEnd = lastDate;

   if(appDelegate.internetActive){
    Webservice *web = [[Webservice alloc]init];
    [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) {
        if(finished){
            [self generateRandomDataForStartDate:startDate endDate:lastDate];
             // NOW return the self.dataArray
        }
    }];
}
}

And your method , which is populating the array, should return the modified array,填充数组的方法应该返回修改后的数组,

    -(NSArray *)generateRandomDataForStartDate:(NSString *)startDate endDate:(NSString *)endDate {
    // Your code here to populate and filling array
      return self.dataArray;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM