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
Filter by
Sorted by
Tagged with
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?
Egil's user avatar
  • 8,141
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 ...
mjisrawi's user avatar
  • 7,926
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 ...
tommi's user avatar
  • 6,933
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?
gurghet's user avatar
  • 7,651
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 ...
Patrick Perini's user avatar
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 ...
XJones's user avatar
  • 22k
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 ...
tom's user avatar
  • 14.5k
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 ...
Jonathan Sterling's user avatar
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 (^...
Jay Dub's user avatar
  • 1,682
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 ...
Paul Wheeler's user avatar
  • 19.5k
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?
Jacksonkr's user avatar
  • 31.9k
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 ...
Bogatyr's user avatar
  • 19.3k
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 ...
cfischer's user avatar
  • 24.7k
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) ...
Rits's user avatar
  • 5,125
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 ...
HanXu's user avatar
  • 5,567
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 ...
Piotr Czapla's user avatar
  • 26.1k
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?
user2206906's user avatar
  • 1,310
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 ...
zoul's user avatar
  • 103k
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: -...
Marty's user avatar
  • 1,087
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 ...
Chris's user avatar
  • 2,488
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 ...
Kyle's user avatar
  • 17.5k
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:...
Ben Scheirman's user avatar
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()...
zoul's user avatar
  • 103k
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 ...
eonil's user avatar
  • 84.7k
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); ...
Marko Zadravec's user avatar
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 ...
Richard J. Ross III's user avatar
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 ...
Bryan's user avatar
  • 5,145
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 ...
noizetoys's user avatar
  • 2,983
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 ...
jemmons's user avatar
  • 18.6k
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 (^...
user1559227's user avatar
  • 1,041
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(...
jay's user avatar
  • 3,617
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 ...
Sujay's user avatar
  • 2,510
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 ...
Andrew's user avatar
  • 1,354
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 ...
Shai Mishali's user avatar
  • 9,332
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 ...
Jacob Relkin's user avatar
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 ...
hzxu's user avatar
  • 5,793
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 ...
ShivaPrasad's user avatar
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 ...
Mason's user avatar
  • 7,013
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(...
Robert Atkins's user avatar
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 ...
Brad Larson's user avatar
  • 170k
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;...
fuzzygoat's user avatar
  • 26.1k
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. ...
bromanko's user avatar
  • 928
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 *...
Jesse's user avatar
  • 891
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 ...
keegan3d's user avatar
  • 10.9k
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, ...
Lio's user avatar
  • 4,255
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)...
Alan Zeino's user avatar
  • 4,406
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 ...
Andrew's user avatar
  • 7,651
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 ...
Ælex's user avatar
  • 14.6k
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 ...
pk-nb's user avatar
  • 2,860
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 ...
arango_86's user avatar
  • 4,300

1
2 3 4 5
53