objective-c-blocks

Grand Central Dispatch and unit testing

北慕城南 提交于 2019-12-22 00:29:39
问题 I have written a simple test case that follows Apple's documentation and I am not seeing the results that I'm expecting. Here's the code: - (void)testExample2 { NSLog(@"1"); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"3"); dispatch_semaphore_signal(semaphore); }); NSLog(@"2"); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSLog(@"4"); dispatch_release(semaphore); } I would expect to read: 1, 2, 3, 4 but

EXC_BAD_ACCESS on customised blocks

若如初见. 提交于 2019-12-22 00:26:49
问题 I defined a block in a class: BXButton.h typedef void(^ButtonClicked)(id sender, NSDictionary *userInfo); @property (assign, nonatomic) ButtonClicked onButtonClicked; - (void)setClickBlock:(ButtonClicked)buttonClicked; BXButton.m - (void)setClickBlock:(ButtonClicked)buttonClicked { onButtonClicked = buttonClicked; } - (void)internalButtonClicked { DLog(@"internal clicked"); if (self.onButtonClicked) { onButtonClicked(self, self.userInfo); } } And I tried to call like this in a view controller

Using blocks causes EXC_BAD_ACCESS

元气小坏坏 提交于 2019-12-21 22:38:15
问题 I'd like to use blocks, but it causes me a EXC_BAD_ACCESS after a few calls. My code: - (void) sendBasket { if (currentSendToBasketBlock != nil) { // there's already a webservice going... set the new one as waiting waitingSendToBasketBlock = ^ { WebServicesModel *webServicesModel = [[[WebServicesModel alloc] init] autorelease]; webServicesModel.delegate = self; [webServicesModel sendBasketToServer:currentBasket]; [self showBasketBackground]; }; [waitingSendToBasketBlock copy]; } else {

Returning block that lives on the local stack

故事扮演 提交于 2019-12-21 20:36:01
问题 The clang analyzer can check for returning stack-based memory. dispatch_block_t getPrintBlock (const char *msg) { return ^{ printf("%s", msg); }; } raises this error: returning block that lives on the local stack What does this error mean? 回答1: You need to make a copy of the block to move it to the heap. i.e. something like: dispatch_block_t createPrintBlock (const char *msg) { return Block_copy(^{ printf("%s", msg); }) ; } 回答2: The error means you are returning a value which will be invalid

Why is UIViewController deallocated on the main thread?

岁酱吖の 提交于 2019-12-21 19:11:32
问题 I recently stumbled upon The Deallocation Problem in some Objective-C code. This topic was discussed before on Stack Overflow in Block_release deallocating UI objects on a background thread. I think I understand the problem and its implications, but to be sure I wanted to reproduce it in a little test project. I first created my own SOUnsafeObject (= an object which should always be deallocated on the main thread). @interface SOUnsafeObject : NSObject @property (strong) NSString *title; -

substituting for __weak when not using ARC

╄→гoц情女王★ 提交于 2019-12-21 05:22:14
问题 I have this line of code: __weak NSBlockOperation *weakOperation = operation; which is triggering this compiler error: __weak attribute cannot be specified on automatic variable. Reason for this is I don't have ARC enabled (not ready to make the switch just yet). So from another StackOverFlow question, I was recommended to use: __unsafe_unretained NSBlockOperation *weakOperation = operation; Which makes the error go away, but for the context I'm using it, it's not working (see this question

Which libraries do you need to link against for a clang program using blocks

此生再无相见时 提交于 2019-12-21 05:03:34
问题 I've discovered (below) that I need to use -fblocks when compiling code which uses blocks. What library do I need to link against to let the linker resolve _NSConcreteStackBlock? (On Ubuntu 9.10 AMD64.) chris@chris-desktop:~$ clang ctest.c ctest.c:3:25: error: blocks support disabled - compile with -fblocks or pick a deployment target that supports them void call_a_block(void (^blockptr)(int)) { ^ ctest.c:11:19: error: blocks support disabled - compile with -fblocks or pick a deployment

Nested blocks and references to self

℡╲_俬逩灬. 提交于 2019-12-21 04:50:21
问题 I have a block wherein I use self so I declare a weak reference to self: __weak MyClass *weakSelf = self; Now my questions: I get an error where I define weakSelf and I don't understand what this should mean.: weak attribute can not be specified on an automatic variable Inside my block I pass weakSelf to another block and I am not sure if I now have to do the same thing again like so: __weak MyClass *weakWeakSelf = weakSelf; And then pass weakWeakSelf to that block? 回答1: This is most likely

GCD with NSURLConnection

◇◆丶佛笑我妖孽 提交于 2019-12-21 04:30:59
问题 I am using GCD to send HTTP request asynchronously. Here is the code that doesn't work: dispatch_async(connectionQueue, ^{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection start];//Not working }); the above code is not working at all. I am not getting any call back in NSURLConnectionDelegate's

NSURLConnection with blocks

陌路散爱 提交于 2019-12-21 04:15:14
问题 I'm using [NSURLConnection connectionWithRequest:req delegate:self]; and then I use -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace; -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; -(void)connection:(NSURLConnection *)connection didReceiveResponse: