objective-c-blocks

Can I cancel a Block added to an NSOperationQueue with addOperationWithBlock:?

淺唱寂寞╮ 提交于 2019-12-04 03:17:52
问题 I've read many many articles which say "BLOCKS ARE THE FUTURE!!!". I'm wondering if it relates to running operations in the background. For example, I have a table view which has images that will come from the web. Right now I can get them using +[NSOperationQueue addOperationWithBlock:] . An operation gets sent to the queue when the cell becomes visible. But is there a way to cancel it once the cell gets scrolled off the screen? Or is the only way to do it to subclass NSOperation ? Using

How to set UIActivityViewController Gmail Share Subject different than body

拟墨画扇 提交于 2019-12-04 03:04:08
问题 I am using Gmail Share Extension from Google. I am providing implementation of: - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType; - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController; - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType; For Mail client (from Apple) it goes into delegate method

Why is this CLLocationCoordinate2D variable unassignable?

泪湿孤枕 提交于 2019-12-04 02:30:06
问题 I have a geocoder method and I want it to return the CLLocationCoordinate2D that it generates for me. - (CLLocationCoordinate2D)geocode{ CLGeocoder *geocoder = [[CLGeocoder alloc] init]; CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0,0); [geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) { if([placemarks count]) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; CLLocation *location = placemark.location;

How to pass a block as an argument into another block in Objective C

浪子不回头ぞ 提交于 2019-12-04 02:29:52
I'm trying to define a block that takes a block as an argument. What's wrong with the following line of code? id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^)(void)block) { NSObject *item = nil; block(); return item; }; Why does the compiler keep giving errors like Parameter name omitted and Expected ")" ? id (^cacheResult)(NSString *, id(^)(void)) = ^(NSString *name, id(^block)(void)) { NSObject *item = nil; block(); return item; }; Blocks have similar syntax to function pointers. You have to declare block name after the ^ This is why typedef was invented. Embedding

Repeat count for UIView block-based animation

人盡茶涼 提交于 2019-12-04 01:06:59
I've looked at the methods for block based animation and noticed there is no equivalent parameter or option for [UIView setAnimationRepeatCount:] . What's the simplest way to repeat an animation a fixed number of times? Do you, for instance, chain them using the completion block? Set a completion callback - re-initiate the animation in it - and keep track of the counter yourself. PommeOuest I just asked a similar question and then I read the 2010-11-15 release of the View Programming Guide for iOS. Page 64 caught my attention. In the animation block, one can still use the [UIView

Clarification on Apple's Block Docs?

本小妞迷上赌 提交于 2019-12-03 21:55:23
问题 I am working through some retain-cycle issues with blocks/ARC, and I am trying to get my head around the nuances. Any guidance is appreciated. Apple's documentation on "Blocks and Variables" (http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html) says the following: If you use a block within the implementation of a method, the rules for memory management of object instance variables are more subtle: If you access an instance variable by

NSOperationQueue - Getting Completion Call Too Early

帅比萌擦擦* 提交于 2019-12-03 21:50:52
I am using a NSOperationQueue to queue and call a number of Geocoding location lookups. I want to call a completion method when all asynchronicly running lookups have been finished. -(void)geocodeAllItems { NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init]; [geoCodeQueue setName:@"Geocode Queue"]; for (EventItem *item in [[EventItemStore sharedStore] allItems]) { if (item.eventLocationCLLocation){ NSLog(@"-Location Saved already. Skipping-"); continue; } [geoCodeQueue addOperationWithBlock:^{ NSLog(@"-Geocode Item-"); CLGeocoder* geocoder = [[CLGeocoder alloc] init]; [self

does dispatch_async copy internal blocks

瘦欲@ 提交于 2019-12-03 21:05:52
问题 Given the following (manual reference counting): void (^block)(void) = ^ { NSLog(@"wuttup"); } void (^async_block)(void) = ^ { block(); } dispatch_async(dispatch_get_main_queue(), async_block); Will "block" be copied rather than thrown off the stack and destroyed? 回答1: I believe, the answer is Yes. The outer block will be asynchronously dispatched which causes the runtime to make a copy on the heap for this block. And as shown below, and described in the Block Implementation Specification -

How to implement an NSRunLoop inside an NSOperation

风流意气都作罢 提交于 2019-12-03 18:30:19
问题 Im posting this question because I have seen a lot of confusion over this topic and I spent several hours debugging NSOperation subclasses as a result. The problem is that NSOperation doesnt do you much good when you execute Asynchronous methods which are not actually complete until the asynchronous callback completes. If the NSOperation itself is the callback delegate it may not even be sufficient to properly complete the operation due to the callback occurring on a different thread. Lets

Are Objective-C blocks autoreleased?

拜拜、爱过 提交于 2019-12-03 17:01:23
问题 If I declare a block like this ^{ DoSomething; } and put it in an instance variable, do I need to Block_copy() if I'm going to keep it around? 回答1: Yes, you need to copy. Not because they are autoreleased, but because they start on the stack. Note that blocks also behave like regular Objective-C objects, so that you can copy them using the regular copy message: void storeBlockForLater: (dispatch_block_t) block { [someArray addObject:[[block copy] autorelease]]; } Or, if you have a block