简体   繁体   中英

return pointer within a GCD block

Here is my case:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

        dispatch_async(backgroundQueue, ^{
         //Do long-running tasks
        if(/*some condition*/){
             //Continue long-running tasks
                    dispatch_async(dispatch_get_main_queue(), ^{
             //UIKit stuff
            });
        return NO;
        }else{
             //Continue long-running tasks

              dispatch_async(dispatch_get_main_queue(), ^{
             //UIKit stuff
            });
         return YES;
}
});
}

Since the block is void return type, I got this compiler error:

Incompatible block pointer types passing 'BOOL(^)(void)' to parameter of type 'dispatch_block_t' (aka 'void(^)(void)')

How to solve that?

Put your condition on the main thread and only do your long lasting task on the worker thread. Otherwise it makes no sense to dispatch on a worker thread since you have to return immediately from textFieldShouldReturn which means you either block the main thread or do as I suggested above.

I don't think it is possible what you wish to accomplish. At least not in this form.

Maybe you can tell us more about what you try to accomplish and we can provide an alternate solution.

I am curious why would you want a long duration task executed in textFieldShouldReturn: which is supposed to return a value immediately. It doesn't matter that you execute it asynchronously, the text field needs a value at that moment.

EDIT

Ok, I understand you must make a long duration search in coredata. Unfortunately textFieldShouldReturn: needs a return value.

textFieldShouldReturn: is just a delegate method that allows you to implement custom behaviour when pressing the Return key. Most commonly, the developer hides the keyboard.

My guess is that you are making a search in coredata before you can decide whether to hide the keyboard. In this case, I would

So, in textFieldShouldReturn: I suggest you hide the keyboard and somehow lock that keyboard to prevent editing. From that method you also trigger the coredata search async. When you get a response, you can decide whether to start editing that text field again (show the keyboard) or just move on.

How does it sound? Does it match your desired behavior?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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