objective-c-blocks

Can Swift return value from an async Void-returning block?

不想你离开。 提交于 2019-12-17 06:13:27
问题 I want to create a function to check if user_id is already in my database. class func checkIfUserExsits(uid:String) -> Bool { userRef.childByAppendingPath(uid).observeSingleEventOfType(.Value, withBlock: { (snapShot: FDataSnapshot!) -> Void in if snapShot.value is NSNull { return false } else { return true } }) } However, observeSingleEventOfType is a API provided by 3rd party Firebase. It is defined to return Void . (void)observeSingleEventOfType:(FEventType)eventType withBlock:(void ( ^ ) (

Custom completion block for my own method [duplicate]

╄→гoц情女王★ 提交于 2019-12-17 04:11:15
问题 This question already has answers here : How can I create my own methods which take a block as an argument and which I can call later? (2 answers) Closed 4 years ago . I have just discovered completion blocks: completion:^(BOOL finished){ }]; What do I need to do to have my own method take a completion block? 回答1: 1) Define your own completion block, typedef void(^myCompletion)(BOOL); 2) Create a method which takes your completion block as a parameter, -(void) myMethod:(myCompletion)

Waiting until two async blocks are executed before starting another block

ⅰ亾dé卋堺 提交于 2019-12-16 22:09:28
问题 When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that? We tried the following, but it doesn't seem to work: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block1 }); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block2 }); // wait until both the block1 and block2 are done before start block3 // how to do that? dispatch

Objective C callbacks with blocks

佐手、 提交于 2019-12-13 23:41:22
问题 I have looked at various answers on SO for this but can't quite get my head around how it all actually works. What I have is a GameEngine that contains touchable elements, what I want is for when an element is touched it fires off a "I have been touched" event that the GameEngine listens for and deals with appropriately. In C# I'd do this with delegates/events but can't seem to find a decent obj c equivalent that uses blocks. - I want to use Blocks as it more akin to anonymous functions in C#

Capturing an Objective-C object weakly within C block without declaring an explicit __weak or __block variable

混江龙づ霸主 提交于 2019-12-13 21:14:00
问题 I am capturing a method-scoped object in a C block and I want to avoid retain cycles. Here is my code: ( balloon is a custom view created within my current method) balloon.onAddedToViewHierarchy = ^{ CGRect globalRect = [[UIApplication sharedApplication].keyWindow convertRect:self.frame fromView:self.superview]; CGRect targetFrame = CGRectMake(20, 0, SCREEN_WIDTH - 40, 60); targetFrame.origin.y = below ? globalRect.origin.y + globalRect.size.height + 10 : globalRect.origin.y - 10 - 60/*height

Xcode, ensure codes after blocks run later for NSURLConnection asynchronous request

岁酱吖の 提交于 2019-12-13 18:33:01
问题 Hi there: I have been writing an iOS program which uses many http queries to the backend rails server, and hence there are tons of codes like below. In this case, it is updating a UITableView : //making requests before this... NSOperationQueue* queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) { NSLog(@"Request sent!"); NSHTTPURLResponse* httpResponse =

How does one instantiate a block as an instance variable using ARC?

亡梦爱人 提交于 2019-12-13 16:39:37
问题 What does one do to make something like this work? void (^)(void) *someBlock = ^{ //some code }; 回答1: Dmitry's answer is exactly right. Think of the block syntax as a C function declaration: // C function -> <return type> <function name> (<arguments>) void someFunction(void) { // do something } // block -> <return type> (^<block variable name>) (<arguments>) void (^someBlock)(void) = ^{ // do something }; Another example: // C function int sum (int a, int b) { return a + b; } // block int (

Observing UIView changes crash

China☆狼群 提交于 2019-12-13 14:51:39
问题 i've used the following code below to determine when views get added to a superview: //Makes views announce their change of superviews Method method = class_getInstanceMethod([UIView class], @selector(willMoveToSuperview:)); IMP originalImp = method_getImplementation(method); void (^ block)(id, UIView *) = ^(id _self, UIView *superview) { [_self willChangeValueForKey:@"superview"]; originalImp(_self, @selector(willMoveToSuperview:), superview); [_self didChangeValueForKey:@"superview"]; };

What is the main difference using “copy” and “strong” ownership qualifiers in property declaration with block types?

寵の児 提交于 2019-12-13 12:18:05
问题 Example #1 @property (nonatomic, copy) void (^errorBlock) (NSError *); Example #2 @property (nonatomic, strong) void (^errorBlock) (NSError *); I know that blocks are standard variables on stack, and by making a copy we are "moving" them to the heap. Thats all? Or not? 回答1: There should be no difference. Since the property has a block type, according to http://clang.llvm.org/docs/AutomaticReferenceCounting.html#blocks With the exception of retains done as part of initializing a __strong

Objective C memory management with blocks, ARC and non-ARC

岁酱吖の 提交于 2019-12-13 11:47:26
问题 I have been using blocks for some time now, but I feel that there are things I miss about memory management in both ARC and non-ARC environments. I feel that deeper understanding will make me void many memory leaks. AFNetworking is my main use of Blocks in a particular application. Most of the time, inside a completion handler of an operation, I do something like "[self.myArray addObject]". In both ARC and non-ARC enabled environments, "self" will be retained according to this article from