【问题标题】:Return an array after a completion block has finished在完成块完成后返回一个数组
【发布时间】:2014-01-06 10:19:12
【问题描述】:

我有以下功能

- (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 中。所以目前该函数总是返回一个空数组。

【问题讨论】:

  • self.dataArray 你被声明它是如何操作任何其他数据
  • 您误解了块的工作原理,从评论返回的内容取决于块的返回类型\
  • 你需要阻塞函数的执行直到完成的块被执行或者从 generateRandomDataForStartDate:startDate 方法返回数组
  • 你为什么在那里使用块?您可以进行同步 Web 服务调用并返回数组。此处的 Web 服务调用是异步的,因此控件将在调用后立即返回。

标签: ios iphone objective-c objective-c-blocks


【解决方案1】:

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

调用者对象将编写以下代码:

[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 接收。

【讨论】:

  • 我认为这是要走的路!你能帮我看看我应该如何处理完成吗?
  • @StefGeelen 你让它运行了吗?
【解决方案2】:

你不需要从这个方法返回一个数组,正如你提到的,你的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
        }
    }];
}
}

你的方法,它正在填充数组,应该返回修改后的数组,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多