Questions tagged [objective-c-blocks]

Blocks are Apple’s implementation of closures for C, which are also available for Objective-C and C++.

Blocks are an Apple extension to C, often used in conjunction with Objective-C. When used from Objective-C, blocks also function as full Objective-C objects.
They are more traditionally known as closures. As closures, they can capture variables from the surrounding scope, be passed to functions, and be stored in variables. The syntax of a Block type is nearly identical to a function pointer, with * being replaced by ^.

An example of block definition is the following one:

int (^minusOne)(int);

minusOne = ^(int anInt) {
    return anInt - 1;
};

For more examples regarding how to declare various types of blocks, see How Do I Declare A Block In Objective-C?, and for more in-depth information, see Blocks Programming Topics.

2629 questions
1
vote
1 answer

ARC: correct use of __unsafe_unretained __block in variable declaration

The design pattern below appears a few times in my app. I'm in the midst of converting to ARC. Can someone corroborate whether __unsafe_retained __block is correct usage? __unsafe_unretained __block id observer = [[NSNotificationCenter…
1
vote
0 answers

How to handle HTTPS request using NSURLConnection sendAsynchronousRequest

I have a HTTPS request in my app and I am doing that way:- NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; For Authentication, I have implemented the following…
1
vote
2 answers

Blocks and extra retains

When ARC enabled, following code causes the obj retained twice for the block by calling objc_retain() and objc_retainBlock().(So retainCount becomes 3 after the block definition.) I checked that obj is not in autorelease pool and there are two…
1
vote
2 answers

block syntax for calling class method in AFNetworking2

This is a total noob question but I'm testing out AFNetworking2 specifically, the UIWebView+AFNetworking piece. But how do I call loadRequest. I have: NSURL *websiteUrl = [NSURL URLWithString:@"http://www.google.com"]; //fine NSURLRequest…
timpone
  • 19,235
  • 36
  • 121
  • 211
1
vote
1 answer

How to execute block inside another block (w/o bad access)

One of my methods (mySecondMethod) receives a block and need to add an additional treatment to this block before passing it as an argument to another method. Here is the code sample: - (void)myFirstMethod { // some code __weak MyController…
Aurelien Porte
  • 2,692
  • 27
  • 32
1
vote
0 answers

Best way to return from a method when inside a synchronously executed block in objective-c?

For synchronizing access to resources, I need to use a certain serial queue when checking for a condition which decides if a method should return or not. The rest of the method does not need to be executed on the serial queue, thus I would like to…
1
vote
2 answers

Best way to manage many block calls

I am developing an app and when it starts its execution it has to get some data from the webService, categories, Image of loading(it changes sometimes), info "how to use" ( also can change in the server, client specifications..). To get this data I…
1
vote
1 answer

Properly using blocks for callbacks between two classes

I have simple app with two views: the first view has a button, and when you press it you get a modal segue to another view. As you know, the controller popped out with a modal segue doesn't have a "Back" button, so you probably write delegates. I…
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
1
vote
2 answers

Using self inside block crashes without error

I have a Class called ServiceBrowser. Inside this class I have a block based method that searches for NSNetServices. I call the method as such : [_serviceBrowser discoverServicesOfType:@"_theService._tcp." …
Ste Prescott
  • 1,789
  • 2
  • 22
  • 43
1
vote
4 answers

Background task with Completion block in IOS

I am doing some database operations in IOS. Basically I want to do this in a background thread. I tried using GCD. But the issue for me is I want to get some values from this process after it is finished. Say before inserting an item to database I…
Zach
  • 9,989
  • 19
  • 70
  • 107
1
vote
3 answers

Should I use a weak references with properties of a property and blocks / ARC?

I know that when we're using ARC and blocks, we should use __weak to prevent capturing strongly self and prevent a retain cycle ! But I was wondering if in the following example I need to use __weak ? __weak MyViewController *weakSelf =…
1
vote
3 answers

When *exactly* is it necessary to copy a block in objective-C under ARC?

I've been getting conflicting information about when I need to copy a block when using ARC in objective-C. Advice varies from "always" to "never", so I'm really not sure what to make of it. I happen to have a case I don't know how to…
Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
1
vote
3 answers

Extending UIRefreshControl with block in a category

I really like the way blocks work and thought it would be nice to add them in a few place like setting the action for UIRefreshControl. So I created a category to UIRefreshControl @interface UIRefreshControl (Blocks) @property (nonatomic, copy)…
ItsASecret
  • 2,589
  • 3
  • 19
  • 32
1
vote
0 answers

SLComposeViewController won't dismiss its parent VC

I'm presenting a SLComposeViewController for sending a facebook message. Presenting and posting works fine and the completion block is getting called (all logs are there). The problem is, when the posting is complete, I also want to dismiss the…
Callmeed
  • 4,972
  • 5
  • 34
  • 49
1
vote
2 answers

Synchronization callbacks - block vs GCD queue

I am new to asynchronous callbacks and have been given differing advice. I need to perform asynchronous callbacks and after many hours of research I still do not know if I should use blocks or GCD and queues. Any pointers would be welcome. OK. So…
Patricia
  • 5,019
  • 14
  • 72
  • 152