回调块中的多个返回值

时间:2015-06-11 14:34:54

标签: ios objective-c callback block

我正在与之合作的人不想使用委托方法有几个原因,他们希望我想出一个解决方案;

基本上他们想要一个带有多个返回值的回调块,当第一个返回值到达时,block仍然应该等待即将到来的返回值

类似

[parserobject parsefile:file withCallback {
                if (started) {
                    //file parsing started 
                    //this should get executed first
                }
                //if file parsing succesfully started also check if it 
                //succesfully finished or failed in the same callback
                if (finished) {
                    //file parsing finished 
                }

                if(failed)
                {
                    //this represents if parsing failed any time from start to finish
                    //if so exit the callback

                }
            }];

我看到有人使用structsnsintegers来返回不同的值,但通常只返回一个结果....

上面的代码块是否可以用objective-c编写?

2 个答案:

答案 0 :(得分:1)

这是你的功能

typedef void(^VOIDCALLBACK)(void); //Just a type here
typedef void(^BOLLCALLBACK)(bool succeed);//Just a type here
-(void)parserobjectWithFile:(NSString *)file StartCallBack:(VOIDCALLBACK) startCallBack completionBlock:(BOLLCALLBACK) completion{
    // When start
 dispatch_async(dispatch_get_main_queue(), ^{
     startCallBack();//Make call back run in main queue
   });
    //When finisehd set succeed to yes,otherwise set to no
    BOOL succeed = YES;//Pass in to indicator if succeed
  dispatch_async(dispatch_get_main_queue(), ^{
    completion(succeed); //Make call back run in main queue
});
}

然后你这样称呼它

   [self parserobjectWithFile:@""
             StartCallBack:^{
                 //Here task is start
             }
           completionBlock:^(bool succeed) {
                //Here task is done.Check succeed to figure out if succeed
           }];

一个简单的例子

这是功能部分

@implementation ViewController

-(void)viewDidAppear:(BOOL)animated{
 [self parserobjectWithFile:@"" StartCallBack:^{
  NSLog(@"start");
 } completionBlock:^(bool succeed) {
   NSLog(@"finished");
 }];
}


typedef void(^VOIDCALLBACK)(void); //Just a type here
typedef void(^BOLLCALLBACK)(bool succeed);//Just a type here
-(void)parserobjectWithFile:(NSString *)file StartCallBack:   (VOIDCALLBACK) startCallBack completionBlock:(BOLLCALLBACK) completion{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Start
    dispatch_async(dispatch_get_main_queue(), ^{
        startCallBack();//Make call back run in main queue
    });
    sleep(2.0);
    BOOL succeed = YES;//Pass in to indicator if succeed
    dispatch_async(dispatch_get_main_queue(), ^{
        completion(succeed); //Make call back run in main queue
    });
});    
}
@end

这将输出

2015-06-11 23:39:25.426 OCTest[767:144615] start
2015-06-11 23:39:27.426 OCTest[767:144615] finished

答案 1 :(得分:0)

根据到目前为止所达到的状态,可以编写一个执行不同操作的块。为了使其起作用,使用__block修饰符声明表示块状态的变量:

__block BOOL started = NO;
__block BOOL finished = NO;
__block BOOL failed = NO;
[parserobject parsefile:file withCallback:^ {
    if (started) {
        //file parsing started 
        //this should get executed first
    } else {
        ...
        // Conditionally set started to YES
        started = YES;
    }
    //if file parsing succesfully started also check if it 
    //succesfully finished or failed in the same callback
    if (finished) {
        //file parsing finished 
    }
    if(failed) {
        //this represents if parsing failed any time from start to finish
        //if so exit the callback
    }
}];