Tag: block

How to call a method once the completion block finish?

I want to call the signUp method first, once I got the userID, I need to call the another method normalSignupMethod. [ConnectionObj signUp:user]; [helper normalSignupMethod:dict]; signUp Method: [MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) { // Sign up was successful // Store user id [SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:@”%@”,response.data[@”id”]]; } errorBlock:^(QBResponse *response) { // Handle error […]

“Converting” a function pointer to a block in objective-C

I’m doing some Interop from Mono C# to Obj-C and ran into this problem. The C# code needs to pass a callback – which it does with a function pointer. I can get the function pointer from the Obj-C side and call it and everything works. But I now need to give that function pointer […]

ObjC: BAD ACCESS when call blocks ^{} in later functions?

Following this discussion, I’ve encountered a bad access issue; A loop has several steps: a, b, c, … x, y, z: -(void)cycle:(float)delta{ [self stepA] [self stepB] // etc. [self stepZ] } At some point, step x does the following: // IRQ is an NSMutableArray // Self is a reference to the engine running the cycles […]

Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

Right now I’m doing a huge refactory to my code and I want to unify the way I’m using Restkit. I had separated ways of making RPC calls to my API server and REST calls. REST calls where made using objectMapping and RPC calls where made with the RKClient. Besides that, I’m using blocks instead […]

iOS GCD Sync with Async Block

I have an async function with a block : [self performAsyncTaskCompletion:(void(^) () ) { //Do Something } ]; I need to call this function many times but in a synchronous way. I tried to use GCD queues : dispatch_queue_t queue = dispatch_queue_create(“com.MyApp.task”, NULL); for (int i = 0; i < array.count; i++) { dispatch_sync(queue, ^{ […]

Why do NSBlocks have to be copied for storage in containers?

– (void) addABlock { void (^aBlock)(void) = ^() { [someObject doSomething]; }; [self.myMutableArray addObject: aBlock]; // Oops.. [self.myMutableArray addObject: [aBlock copy]]; // works fine } In the above simplified example I am seeing undefined behavior if the block copy is not performed. This case is specifically listed in apple’s ARC transition guide. The part that […]

What is the purpose of using blocks

I want to use blocks in my application, but I don’t really know anything about blocks. Can anyone explain how and why I should use blocks in my code?

How can I create my own methods which take a block as an argument and which I can call later?

How can I create my own methods which take a block as an argument and which I can call later? I have tried following things. #import <UIKit/UIKit.h> typedef void (^viewCreator)(void); @interface blocks2ViewController : UIViewController { } -(void)createButtonUsingBlocks:(viewCreator *)block; @end – (void)viewDidLoad { [super viewDidLoad]; [self createButtonUsingBlocks:^(NSString * name) { UIButton *dummyButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, […]

How do I create an objective-c method that return a block

-(NSMutableArray *)sortArrayByProminent:(NSArray *)arrayObject { NSArray * array = [arrayObject sortedArrayUsingComparator:^(id obj1, id obj2) { Business * objj1=obj1; Business * objj2=obj2; NSUInteger prom1=[objj1 .prominent intValue]; NSUInteger prom2=[objj2 .prominent intValue]; if (prom1 > prom2) { return NSOrderedAscending; } if (prom1 < prom2) { return NSOrderedDescending; } return NSOrderedSame; }]; NSMutableArray *arrayHasBeenSorted = [NSMutableArray arrayWithArray:array]; return arrayHasBeenSorted; } […]

What is the proper way to avoid Retain Cycle while using blocks

What is the proper way to add objects in NSMutableArray which is strongly defined by property. [tapBlockView setTapBlock:^(UIImage* image) { [self.myImageArray addObject:image]; // self retain cycle } If I will create weak reference something like __weak NSMutableArray *array = self.myImageArray; [tapBlockView setTapBlock:^(UIImage* image) { [array addObject:image]; // If I will do this then how will […]