objective-c-blocks

Geocoding Multiple Locations - Knowing When “All” Completion Blocks Have Been Called

梦想的初衷 提交于 2019-12-06 11:29:39
I am using the CoreLocation's geocoder to get the CLLocation coordinates for multiple map items. The geocoder calls a completion block on completion for each item. How do I create a similar block functionality which is called when all of these containing asynchronous geocoder calls have been completed? (I could use a manual counter. But there must be a more elegant solution) Here's my geocoding function so far. It loops through an array of location items and starts a new geocoding process for each. -(void)geoCodeAllItems { for (EventItem* thisEvent in [[EventItemStore sharedStore] allItems]) {

call a method in a block?

故事扮演 提交于 2019-12-06 11:01:26
I'm new to ARC. I want to call a method in a complete block, but I get the warning: Capturing 'self' strongly in this block is likely to lead to a retain cycle. . Code: - (void) handlerComplete { //... } - (void) loadData { ... operation.completeBlock = ^(NSInteger index) { [self handlerComplete]; }; } Any advice? Thanks. Try with - (void) loadData { __weak MyClassType *myClass = self; operation.completeBlock = ^(NSInteger index) { [myClass handlerComplete]; }; } Make a weak reference instead: operation.completeBlock = ^(NSInteger index) { __weak Foo *bar = self; [bar handlerComplete]; }; I

Repeating UIAnimation block, and a way to stop it again

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 07:56:53
问题 I wanted to do a small loader animation to put in my app. I have done repeating animations before with CGAnimations without problems, this time I was going for the block approach. I am doing a small test but can make the following code repeat: - (void) startLoading { __block int count = 0; [UIView animateWithDuration:0.4 delay: 0.0 options: UIViewAnimationOptionRepeat animations:^{ count++; } completion:^(BOOL finished){ if (count > 5) count = 0; NSLog(@"%d", count); }]; } - (void)

crashing at performBatchUpdates of collection view [closed]

孤街醉人 提交于 2019-12-06 06:54:44
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . i have a collection view which display two array of images with different section count's this will be toggled between two different view of one collectionview view - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { if (self.storyview) { return 4; } else { return 1; } } Where i

Could you help me to understand block types when added to containers (NSDictionary, NSArray)?

独自空忆成欢 提交于 2019-12-06 06:47:04
Normally blocks can be of 3 types: NSGlobalBlock, NSStackBlock, NSMallocBlock. Lets take the following example: void (^aBlock)(NSString *someString) = ^(NSString *someString){ NSLog(@"Block was executed. %@", someString); }; NSDictionary *dictionary = [NSDictionary dictionaryWithObject:aBlock forKey:@"aBlock"]; Because aBlock doesn't capture surrounding scope if I do po dictionary I get aBlock = < NSGlobalBlock :0x165dde60> and this is correct If I then do a: NSString *string = @"Test"; void (^aBlock)(NSString *someString) = ^(NSString *someString){ NSLog(@"Block was executed. %@ %@",

Block automatic retaining, does it affect even to ivars in self?

*爱你&永不变心* 提交于 2019-12-06 05:54:40
问题 If I have class: @interface A : NSObject { BOOL b; id c; } @end and reference b and c in a block, is the block retain self automatically? Or just b and c ? About c , it may be retained itself, but how about b ? 回答1: Bill's answer isn't quite correct: If you have an instance of A and create a block inside of that instance like so: ^{ b = YES; } Then self is retained (when the block is copied). b is not const -copied, because b is strongly referenced by self , and only self is const within the

How to update UI in a task completion block?

浪子不回头ぞ 提交于 2019-12-06 05:54:33
问题 In my application, I let a progress indicator starts animation before I send a HTTP request. The completion handler is defined in a block. After I get the response data, I hide the progress indicator from inside the block. My question is, as I know, UI updates must be performed in the main thread. How can I make sure it? If I define a method in the window controller which updates UI, and let the block calls the method instead of updating UI directly, is it a solution? 回答1: Also, if your app

What is a block in C++? [duplicate]

故事扮演 提交于 2019-12-06 04:12:09
This question already has an answer here : Closed 7 years ago . Possible Duplicate: What is (double (^)(int))foofoo I've tried searching for a definition on Google and SO and I found examples in which they are used but not clearly defined. By "block" I mean the caret symbol ( ^ ). I found it on a site where cdecl they described : (double (^)(int, long long )) foo as cast foo into block(int, long long) returning double I've never once seen this symbol used before today. Can anyone clearly describe what a block is and include with it a minimal working example? Thanks. Blocks are a non-standard

Objective C : class init with block ?

谁都会走 提交于 2019-12-06 03:50:59
问题 Is it possible to, say, use a block as a completion handler in a View Controller's init method so that the parent view controller is able to fill in the details in a block without having to create a custom initWithNibName:andResourceBundle:andThis:andThat: for each possible properties ? // ... in the didSelectRowAtIndexPath method of the main view controller : SubViewController *subviewController = [[SubViewController alloc] initWithNibName:nil bundle:nil completionHandler:^(SubViewController

static variables inside Objective-C blocks?

拥有回忆 提交于 2019-12-06 03:33:35
问题 I'm working on some CoreAnimation stuff. A navigation controller with a few view controllers. And the view controllers have UISCrollViews for different "pages" of a "brochure." On each page, there might be some animation that gets triggered when the user flips to that page. I was trying something like this (for one-shot animations). void (^animationBlock)() = ^{ static bool alreadyTriggered = NO; if(alreadyTriggered) return; [CATransaction begin]; [CATransaction setCompletionBlock: ^{