Questions tagged [objective-c-blocks]
Blocks are Apple’s implementation of closures for C, which are also available for Objective-C and C++.
                                	
	objective-c-blocks
    
                            
                        
                    
            2,628
            questions
        
        
            776
            votes
        
        
            20
            answers
        
        
            399k
            views
        
    How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?
                Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float?
            
        
       
    
            484
            votes
        
        
            8
            answers
        
        
            123k
            views
        
    What does the "__block" keyword mean?
                What exactly does the __block keyword in Objective-C mean? I know it allows you to modify variables within blocks, but I'd like to know... 
What exactly does it tell the compiler?
Does it do anything ...
            
        
       
    
            407
            votes
        
        
            8
            answers
        
        
            163k
            views
        
    Assign a variable inside a Block to a variable outside a Block
                I'm getting an error 
  Variable is not assignable (missing __block type specifier)
on the line aPerson = participant;. How can I make sure the block can access the aPerson variable and the aPerson ...
            
        
       
    
            331
            votes
        
        
            8
            answers
        
        
            127k
            views
        
    Can I use Objective-C blocks as properties?
                Is it possible to have blocks as properties using the standard property syntax?
Are there any changes for ARC?
            
        
       
    
            282
            votes
        
        
            7
            answers
        
        
            85k
            views
        
    Block Declaration Syntax List
                Block syntax in Objective C (and indeed C, I presume) is notoriously incongruous. Passing blocks as arguments looks different than declaring blocks as ivars, which looks different than typedefing ...
            
        
       
    
            221
            votes
        
        
            8
            answers
        
        
            82k
            views
        
    How do I avoid capturing self in blocks when implementing an API?
                I have a working app and I'm working on converting it to ARC in Xcode 4.2. One of the pre-check warnings involves capturing self strongly in a block leading to a retain cycle. I've made a simple code ...
            
        
       
    
            203
            votes
        
        
            10
            answers
        
        
            136k
            views
        
    Waiting until two async blocks are executed before starting another block
                When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that?
We tried the following, but it doesn't ...
            
        
       
    
            169
            votes
        
        
            9
            answers
        
        
            60k
            views
        
    Retain cycle on `self` with blocks
                I'm afraid this question is pretty basic, but I think it's relevant to a lot of Objective-C programmers who are getting into blocks.
What I've heard is that since blocks capture local variables ...
            
        
       
    
            158
            votes
        
        
            8
            answers
        
        
            132k
            views
        
    Store a closure as a variable in Swift
                In Objective-C, you can define a block's input and output, store one of those blocks that's passed in to a method, then use that block later:
// in .h
    typedef void (^...
            
        
       
    
            151
            votes
        
        
            6
            answers
        
        
            67k
            views
        
    When to use enumerateObjectsUsingBlock vs. for
                Besides the obvious differences:
Use enumerateObjectsUsingBlock when you need both the index and the object
Don't use enumerateObjectsUsingBlock when you need to modify local variables (I was wrong ...
            
        
       
    
            151
            votes
        
        
            10
            answers
        
        
            109k
            views
        
    Objective-C pass block as parameter
                How can I pass a Block to a Function/Method?
I tried - (void)someFunc:(__Block)someBlock with no avail.
ie. What is the type for a Block?
            
        
       
    
            148
            votes
        
        
            5
            answers
        
        
            91k
            views
        
    Declare a block method parameter without using a typedef
                Is it possible to specify a method block parameter in Objective-C without using a typedef?   It must be, like function pointers, but I can't hit on the winning syntax without using an intermediate ...
            
        
       
    
            104
            votes
        
        
            7
            answers
        
        
            56k
            views
        
    Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?
                I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called.
For the ...
            
        
       
    
            88
            votes
        
        
            6
            answers
        
        
            39k
            views
        
    Blocks instead of performSelector:withObject:afterDelay: [duplicate]
                I often want to execute some code a few microseconds in the future. Right now, I solve it like this:
- (void)someMethod
{
    // some code
}
And this:
[self performSelector:@selector(someMethod) ...
            
        
       
    
            82
            votes
        
        
            4
            answers
        
        
            33k
            views
        
    What is the difference between a __weak and a __block reference?
                I'm reading Xcode's documentation, and here is something that puzzles me:
__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
    [tmpSelf doSomething];
}];
The following is copied ...
            
        
       
    
            80
            votes
        
        
            3
            answers
        
        
            42k
            views
        
    How to store blocks in properties in Objective-C?
                I'd like to store objective-c block in a property for later use. I wasn't sure how to do it so I googled a bit and there is very little info about the subject. But I've managed to find the solution ...
            
        
       
    
            79
            votes
        
        
            3
            answers
        
        
            70k
            views
        
    Custom completion block for my own method [duplicate]
                I have just discovered completion blocks: 
 completion:^(BOOL finished){
                     }];
What do I need to do to have my own method take a completion block?
            
        
       
    
            74
            votes
        
        
            4
            answers
        
        
            16k
            views
        
    Why do nil / NULL blocks cause bus errors when run?
                I started using blocks a lot and soon noticed that nil blocks cause bus errors:
typedef void (^SimpleBlock)(void);
SimpleBlock aBlock = nil;
aBlock(); // bus error
This seems to go against the usual ...
            
        
       
    
            74
            votes
        
        
            6
            answers
        
        
            45k
            views
        
    Calling [self methodName] from inside a block?
                I've just run into blocks and I think they are just what I'm looking for, except for one thing: is it possible to call a method [self methodName] from within a block?
This is what I'm trying to do:
-...
            
        
       
    
            72
            votes
        
        
            3
            answers
        
        
            44k
            views
        
    Implementing a method taking a block to use as callback
                I would like to write a method similar to this:
+(void)myMethodWithView:(UIView *)exampleView completion:(void (^)(BOOL finished))completion;
I've basically stripped down the syntax taken from one ...
            
        
       
    
            70
            votes
        
        
            5
            answers
        
        
            41k
            views
        
    Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior
                Given the following:
- (void) someMethod
{
    dispatch_async(dispatch_get_main_queue(), ^{
        myTimer = [NSTimer scheduledTimerWithTimeInterval: 60
                                              ...
            
        
       
    
            65
            votes
        
        
            10
            answers
        
        
            38k
            views
        
    UIButton block equivalent to addTarget:action:forControlEvents: method?
                I looked around, but couldn't find this on the internet, nor anywhere in the Apple docs, so I'm guessing it doesn't exist.
But is there a iOS4 blocks equivalent API to:
[button addTarget:self action:...
            
        
       
    
            65
            votes
        
        
            3
            answers
        
        
            22k
            views
        
    How to dispatch on main queue synchronously without a deadlock?
                I need to dispatch a block on the main queue, synchronously. I don’t know if I’m currently running on the main thread or no. The naive solution looks like this:
dispatch_sync(dispatch_get_main_queue()...
            
        
       
    
            64
            votes
        
        
            1
            answer
        
        
            10k
            views
        
    Which is the right one, nil or NULL, to mark "no Objective-C block"?
                If I want to pass nothing for an Objective-C block, what keyword should I use, NULL or nil? I'm asking this because an Objective-C block is an Objective-C object (as I know), but represented as a ...
            
        
       
    
            61
            votes
        
        
            2
            answers
        
        
            42k
            views
        
    Using weak self in dispatch_async function
                I read a lot of posts about using __weak self inside dispatch_async, and now I am a litle bit confused.
if I have :
self.myQueue = dispatch_queue_create("com.biview.core_data", NULL);
...
            
        
       
    
            61
            votes
        
        
            6
            answers
        
        
            34k
            views
        
    Why can't we use a dispatch_sync on the current queue?
                I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn't know which until runtime (using StoreKit.framework).
I also had UI ...
            
        
       
    
            53
            votes
        
        
            3
            answers
        
        
            56k
            views
        
    dispatch_sync vs. dispatch_async on main queue
                Bear with me, this is going to take some explaining. I have a function that looks like the one below.
Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that ...
            
        
       
    
            52
            votes
        
        
            2
            answers
        
        
            20k
            views
        
    Storing Blocks in an Array
                In Objective-C, I know that blocks are considered objects, so I was wondering if it was possible to store them in an array.  This begs the question, are blocks first class objects or are they just ...
            
        
       
    
            50
            votes
        
        
            4
            answers
        
        
            21k
            views
        
    How to cancel NSBlockOperation
                I have a long running loop I want to run in the background with an NSOperation. I'd like to use a block:
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
   while(/* not ...
            
        
       
    
            50
            votes
        
        
            2
            answers
        
        
            34k
            views
        
    How does typedef-ing a block works
                In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear.
Doing typedef for a block -typedef void (^MyBlock) (int a);
Now, we can use MyBlock.
Shouldn't it be like - typedef void (^...
            
        
       
    
            49
            votes
        
        
            1
            answer
        
        
            35k
            views
        
    Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)" [duplicate]
                In My dispach_async code block I cannot access global variables. I am getting this error Variable is not Assignable (missing _block type specifier).
NSString *textString;
dispatch_async(...
            
        
       
    
            49
            votes
        
        
            3
            answers
        
        
            25k
            views
        
    Difference between block (Objective-C) and closure (Swift) in iOS
                In tutorials it's written that functionally both are same even closure is more easier then block and its avoided the complexity of block and memory management, I've gone through many tutorials but ...
            
        
       
    
            47
            votes
        
        
            5
            answers
        
        
            83k
            views
        
    How to write an Objective-C Completion Block
                I'm in a situation where need to call a class method from my view controller, have it do it's thing, but then perform some actions ONLY AFTER the class method has completed.
(I think what I need is a ...
            
        
       
    
            47
            votes
        
        
            6
            answers
        
        
            41k
            views
        
    Can AFNetworking return data synchronously (inside a block)?
                I have a function using AFJSONRequestOperation, and I wish to return the result only after success. Could you point me in the right direction? I'm still a bit clueless with blocks and AFNetworking ...
            
        
       
    
            45
            votes
        
        
            2
            answers
        
        
            21k
            views
        
    Block references as instance vars in Objective-C
                I was wondering if it's possible to store a reference to an anonymous function (block) as an instance variable in Objective-C.
I know how to use delegation, target-action, etc. I am not talking about ...
            
        
       
    
            44
            votes
        
        
            3
            answers
        
        
            12k
            views
        
    Objective-C: `continue` in collection enumeration block?
                If I have an NSArray and I use enumerateUsingBlock to loop through elements in the array, but in some cases I need to skip the loop body and go to next element, is there any continue equivalent in ...
            
        
       
    
            42
            votes
        
        
            2
            answers
        
        
            14k
            views
        
    Is it necessary to use weak references to self always inside blocks..?
                I am getting confused with use of self inside blocks, I go through some of Apple's documents but still cannot find the right answer.
Some people always say use weak self inside blocks, but some say ...
            
        
       
    
            42
            votes
        
        
            3
            answers
        
        
            22k
            views
        
    iOS blocks and strong/weak references to self
                I have a question about strong and weak references to self in blocks in iOS. I know the proper way to refer to self inside a block is to create a weak reference outside the block, and then a strong ...
            
        
       
    
            40
            votes
        
        
            4
            answers
        
        
            11k
            views
        
    Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController?
                Say I have the following method inside a UIViewController subclass:
- (void)makeAsyncNetworkCall
{
    [self.networkService performAsyncNetworkCallWithCompletion:^{
        dispatch_async(...
            
        
       
    
            40
            votes
        
        
            2
            answers
        
        
            7k
            views
        
    How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?
                Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function ...
            
        
       
    
            37
            votes
        
        
            3
            answers
        
        
            10k
            views
        
    Executing Blocks From NSArray?
                I was just thinking, as you can treat Blocks like objects if I create two of them and then add them to an NSArray is there a way to execute them from the array?
int (^Block_001)(void) = ^{ return 101;...
            
        
       
    
            37
            votes
        
        
            6
            answers
        
        
            10k
            views
        
    Better asynchronous control flow with Objective-C blocks
                I'm using AFNetworking for asynchronous calls to a web service. Some of these calls must be chained together, where the results of call A are used by call B which are used by call C, etc.
...
            
        
       
    
            36
            votes
        
        
            1
            answer
        
        
            59k
            views
        
    Objective-C callback handler [closed]
                I have a callback method that I got to work, but I want to know how to pass values to it.
What I have is this:
@interface DataAccessor : NSObject
{
    void (^_completionHandler)(Account *...
            
        
       
    
            34
            votes
        
        
            10
            answers
        
        
            27k
            views
        
    Generic typeof for weak self references
                I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles.
When I first read about this it seems that the convention was to use __block ...
            
        
       
    
            34
            votes
        
        
            5
            answers
        
        
            18k
            views
        
    Why should I choose GCD over NSOperation and blocks for high-level applications?
                Apple's Grand Central Dispatch reference says:
  "...if your application needs to operate at the Unix level of the
  system—for example, if it needs to manipulate file descriptors, Mach
  ports, ...
            
        
       
    
            34
            votes
        
        
            3
            answers
        
        
            17k
            views
        
    How to simplify callback logic with a Block?
                Let's say I need to communicate with a class that provides a protocol and calls delegate methods when an operation is complete, as so:
@protocol SomeObjectDelegate
@required
- (void)stuffDone:(id)...
            
        
       
    
            34
            votes
        
        
            4
            answers
        
        
            19k
            views
        
    Objective-C Block Property with Xcode code completion
                Is it possible to define an Objective-C block property but still have full-code completion in Xcode 4?
If I use a typedef to define the block:
typedef void (^CompletionBlock)(MyObject *myObj);
and ...
            
        
       
    
            33
            votes
        
        
            1
            answer
        
        
            23k
            views
        
    FIFO serial queue using GCD
                I am trying to create a (network) synchronized array for the company I work for. While the networking part works fine, I have dwelled into an issue. 
My wish was to create a new queue using ...
            
        
       
    
            33
            votes
        
        
            2
            answers
        
        
            1k
            views
        
    Alternative syntax to __block?
                I have question on the syntax of __block variables. I know you can use __block on a variable in scope so it's not read-only inside the block. However in one spot in the apple docs, I saw an ...
            
        
       
    
            29
            votes
        
        
            3
            answers
        
        
            13k
            views
        
    Do we need to use __weak self inside UIAnimationBlocks in ARC?
                Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak?
[UIView animateWithDuration:animationDuration 
...