完成块完成后返回一个数组 [英] Return an array after a completion block has finished

查看:73
本文介绍了完成块完成后返回一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能

- (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; 
}

我不知道completionblock完成后如何返回self.dataArray.因为我的self.dataArray填充在方法generateRandomDataForStartDate:startDate中.因此,此刻函数始终返回一个空数组.

I can't figure out how I can return the self.dataArray when the completionblock has finished. Because my self.dataArray is filled inside the method generateRandomDataForStartDate:startDate . So at the moment the function always returns an empty array.

感谢先进

推荐答案

您应该在参数内传递完成处理程序块.将返回类型设置为void.

You should pass the completion handler block inside the argument. Make the return type to void.

Caller对象将编写以下代码:

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);
}

在调用方代码句柄完成块中,响应数组作为argumnet收到.

In the caller code handle completion block with response array received as argumnet.

这篇关于完成块完成后返回一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆