Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop BFTask Chain ? #39

Closed
Fykec opened this issue Jun 13, 2014 · 1 comment
Closed

How to stop BFTask Chain ? #39

Fykec opened this issue Jun 13, 2014 · 1 comment

Comments

@Fykec
Copy link

Fykec commented Jun 13, 2014

Hi Guys

I have a question "how to stop the BFTask chain, break from the chain execution"

See Commnets

 BFTask *task = [_model getX1Task];

    [[task continueWithBlock:^id(BFTask *task) {
        if (task.error || task.exception || task.isCancelled) {
            
            return nil;
        } else {
            NSDictionary *dic = (NSDictionary *)task.result;
            if ([dic isKindOfClass:[NSDictionary class]])
            {
                if ([self checkX1OK])
                {
                
                    return [_model getX2Task];
                    
                }
                else
                {
                    //here I want break 
                    //so i return nil, but I found the continueWithBlock will still return a new task have a nil result, so the chain automaticall go to below 
                    return nil
                }
            }
        }
    }] continueWithBlock:^id(BFTask *task) {
    //but the code come here, even the before task is return nil 
        if (task.error || task.exception || task.isCancelled) {
            
            return nil;
        }
        else {
            if ([self checkX2OK])
            {
               
                return [_model getX3Task];
            }
            else
            {
                //here want break 
                //so i return nil
                return nil
            }
        }
    }] ;

I found why the nil case return default task in BFTask's method
continueWithExecutor:withBlock:

- (BFTask *)continueWithExecutor:(BFExecutor *)executor
                       withBlock:(BFContinuationBlock)block {
    BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
    ............................
    //because the result is nil, so it's not a BFTask class
            if ([result isKindOfClass:[BFTask class]]) {
                [(BFTask *)result continueWithBlock:^id(BFTask *task) {
                    ...........
                                        return nil;
                }];
            } else {
                tcs.result = result; //so nil result will go here
            }
        }];
    };
    
    ......
    
    return tcs.task; 
    //so even a nil result will return a default task which is create by BFTaskCompletionSource
}

I have thought a way like this to add one stop:BOOL in the block

typedef id(^BFContinuationWithStopBlock)(BFTask *task, BOOL *stop);
so I can mark &stop = YES, when I want to stop the execution, what are your opinions?

@grantland
Copy link
Member

The best way I would recommend doing this is to use a nested Task:

[task continueWithBlock:^id(BFTask *task) {
  bool continue = // should continue?

  if (continue) {
    return [task continueWithBlock:^id(BFTask *task) {
      // do other things
    }];
  }

  return nil;
}];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants