objective-c-blocks

Force asynchrounous Firebase query to execute synchronously?

ぃ、小莉子 提交于 2019-12-17 21:35:30
问题 I'm designing an app that uses firebase to store user information. Below, I'm trying to write a method that queries the database, obtains the stored password, and checks it against the inputted password, returning a boolean based on whether or not they match. -(BOOL) ValidateUser: (NSString*)username :(NSString*)password { //initialize blockmutable true-false flag __block bool loginFlag = true; //initialize blockmutable password holder __block NSString* passholder; //check if user exists in

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

时光怂恿深爱的人放手 提交于 2019-12-17 18:37:51
问题 -(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

Do we need to use __weak self inside UIAnimationBlocks in ARC?

点点圈 提交于 2019-12-17 17:40:55
问题 Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak? [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ [self doSomething]; } completion:^(BOOL finished) { if (finished) { [self doSomething]; } }]; I am also confused in the following scenario. Any thoughts on this? please share your comments. [self.navController

How can I access a __block variable, after the block has completed? [duplicate]

混江龙づ霸主 提交于 2019-12-17 16:59:35
问题 This question already has answers here : findObjectsInBackgroundWithBlock: gets data from Parse, but data only exists inside the block (2 answers) Closed 5 years ago . I'm doing some background operations with Parse.com, but this is a general question about __block variables. I want to define a variable, run a background network operation with a completion block, possibly modify that variable within the block, then access it outside of the block. But it's always nil. How can I retain the

Why do nil / NULL blocks cause bus errors when run?

强颜欢笑 提交于 2019-12-17 10:12:52
问题 I started using blocks a lot and soon noticed that nil blocks cause bus errors: typedef void (^SimpleBlock)(void); SimpleBlock aBlock = nil; aBlock(); // bus error This seems to go against the usual behaviour of Objective-C that ignores messages to nil objects: NSArray *foo = nil; NSLog(@"%i", [foo count]); // runs fine Therefore I have to resort to the usual nil check before I use a block: if (aBlock != nil) aBlock(); Or use dummy blocks: aBlock = ^{}; aBlock(); // runs fine Is there another

How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

落爺英雄遲暮 提交于 2019-12-17 09:18:13
问题 Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on that queue, so I needed to check for this case in order to prevent a deadlock from a synchronous dispatch to the same queue. I used code like the following to do this: void runSynchronouslyOnVideoProcessingQueue(void (^block)(void)) { dispatch

How to cancel NSBlockOperation

不问归期 提交于 2019-12-17 08:18:08
问题 I have a long running loop I want to run in the background with an NSOperation . I'd like to use a block: NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while(/* not canceled*/){ //do something... } }]; The question is, how to I check to see if it's canceled. The block doesn't take any arguments, and operation is nil at the time it's captured by the block. Is there no way to cancel block operations? 回答1: Doh. Dear future googlers: of course operation is nil when

What is the difference between a __weak and a __block reference?

我的梦境 提交于 2019-12-17 08:10:12
问题 I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf = self; [self methodThatTakesABlock:^ { [tmpSelf doSomething]; }]; The following is copied from the documentation: A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self , so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle, you need

UIButton block equivalent to addTarget:action:forControlEvents: method?

亡梦爱人 提交于 2019-12-17 07:12:10
问题 I looked around, but couldn't find this on the internet, nor anywhere in the Apple docs, so I'm guessing it doesn't exist. But is there a iOS4 blocks equivalent API to: [button addTarget:self action:@selector(tappy:) forControlEvents:UIControlEventTouchUpInside]; I suppose this could be implemented using a category, but would rather not write this myself due to extreme laziness :) Something like this would be awesome: [button handleControlEvent:UIControlEventTouchUpInside withBlock:^ { NSLog(

Creating delegates on the spot with blocks

本小妞迷上赌 提交于 2019-12-17 06:51:30
问题 I love blocks and it makes me sad when I can't use them. In particular, this happens mostly every time I use delegates (e.g.: with UIKit classes, mostly pre-block functionality). So I wonder... Is it possible -using the crazy power of ObjC-, to do something like this? // id _delegate; // Most likely declared as class variable or it will be released _delegate = [DelegateFactory delegateOfProtocol:@protocol(SomeProtocol)]; _delegate performBlock:^{ // Do something } onSelector:@selector