objective-c-blocks

retainCount in blocks show extrange behavior

 ̄綄美尐妖づ 提交于 2019-12-09 03:47:47
问题 I got the this code in a class: - (void)cancel { if (_cancelBlock) _cancelBlock(); } - (void)overrideCancelWithBlock:(void(^)(void))cancelBlock { [_cancelBlock release]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[cancelBlock retainCount]=%lu (before)", [cancelBlock retainCount]); _cancelBlock = [[cancelBlock copy] retain]; NSLog(@"AsyncOperation-overrideCancelWithBlock-[_cancelBlock retainCount]=%lu (after)", [_cancelBlock retainCount]); } - (void)dealloc { NSLog(@"AsyncOperation-dealloc

How to Retrieve the Results of “FBSDKGraphRequest” to Use Outside?

和自甴很熟 提交于 2019-12-08 14:21:37
I am using Facebook SDK for iOS. I can output the results with "NSLog" but I do not know how to retrieve the values of the results from FBSDKGraphRequest outside since they are located in a completion block. I need these values for the later manipulations. I tried to put them in NSArray or NSMutableArray but could not make it. The code for the illustration is given below: __block NSMutableArray *results; __block NSArray *obj; if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"user_groups"]) { NSLog(@"user_groups permission is granted!"); FBSDKGraphRequest *fgr = [[[FBSDKGraphRequest alloc

BOOL method not returning YES inside of block

↘锁芯ラ 提交于 2019-12-08 14:08:42
问题 I have created a new method which returns a BOOL, shown below. +(BOOL)checkIfGameAlreadyExistsAgainst:(PFUser *)opponentUser { // Find all the games where the current user is user1 and the opponentUser is user2 PFQuery *currentUserIsUser1 = [PFQuery queryWithClassName:@"Game"]; [currentUserIsUser1 whereKey:kMESGameUser1 equalTo:[PFUser currentUser]]; [currentUserIsUser1 whereKey:kMESGameUser2 equalTo:opponentUser]; [currentUserIsUser1 whereKey:kMESGameIsActive equalTo:[NSNumber numberWithBool

objective c block that waits for another delegate

纵然是瞬间 提交于 2019-12-08 13:41:28
问题 I have a watchkit app that calls a viewcontroller on an iphone app. I have a delegate for a network connection. I'm trying to use a block so that I don't tightly couple my AppDelegate and my view controller too closely. How can I notify my block when the delegate is finished? ViewController.m -(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{ [self setUpAppForWatch]; completion(YES); } -(void)finishedMessageParse:(NSMutableData *)messageData{ //the delegate is finish tell

Return an array after a completion block has finished

依然范特西╮ 提交于 2019-12-08 12:59:39
问题 I have the following function - (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{ AppDelegate* appDelegate = [UIApplication sharedApplication].delegate; _currentStart = startDate; _currentEnd = lastDate; if(appDelegate.internetActive){ Webservice *web = [[Webservice alloc]init]; [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) { if(finished){ [self generateRandomDataForStartDate

Objective-C / Blocks - Isn't this a retain cycle?

回眸只為那壹抹淺笑 提交于 2019-12-08 09:30:45
问题 @interface ClassA : NSObject @property (strong, nonatomic) dispatch_queue_t dispatchQ; @property (strong, nonatomic) NSString *string; @end @implementation ClassA - (id)init { self = [super init]; if (self) { _dispatchQ = dispatch_queue_create("com.classa.q", NULL); } return self; } - (void)longRunningTaskWithCompletion:(void(^)(void))completion { dispatch_async(self.dispatchQ, ^{ for (int i = 0; i < 10000; i++) { NSLog(@"%i", i); } dispatch_sync(dispatch_get_main_queue(), ^{ self.string = @

Using blocks and ARC is causing a “message sent to deallocated instance” error

老子叫甜甜 提交于 2019-12-08 09:17:47
问题 I had a problem with ARC and blocks but have solved the problem. Unfortunately I don't what exactly is going on and would like to learn more about the situation I had. Originally, I had code that did this for(__block id<Foo> object in objects) { foo download:someParm success:^{ object.state = StateNewState; } ]; } This caused an imbalance in retains. A crash occurs when an object is accessed and is said to be already deallocated. I wrote a class that implemented and used the "copy" attribute

loading images in table using blocks - dispatch_async return value

╄→гoц情女王★ 提交于 2019-12-08 07:27:12
问题 I have looked around on SO and the web and have not found a specific answer. Quite simply, I have a table loading images info from Flickr. I want to display a thumbnail of the image on the left side of each cell. In order to do this without blocking the main (UI) thread, I am using blocks: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"top50places"; UITableViewCell *cell = [tableView

How much overhead does a msg_send call incur?

岁酱吖の 提交于 2019-12-08 07:19:07
问题 I'm attempting to piece together and run a list of tasks put together by a user. These task lists can be hundreds or thousand of items long. From what I know, the easiest and most obvious way would be to build an array and then iterate through them: NSArray *arrayOfTasks = .... init and fill with thousands of tasks for (id *eachTask in arrayOfTasks) { if ( eachTask && [eachTask respondsToSelector:@selector(execute)] ) [eachTask execute]; } For a desktop, this may be no problem, but for an

If a strong property has a block that refers to self, is that a retain cycle?

拜拜、爱过 提交于 2019-12-08 05:48:46
问题 For instance: [self.contentWrapperView addGestureRecognizer: [UITapGestureRecognizer recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) { if (self.customEditing) { [self setEditingMode:NO Animated:YES]; } }]]; Where contentWrapperView is a strong property on self , and presuming contentWrapperView has a strong reference to the recognizer block. Is using self in the block going to lead to a retain cycle? This is the only part I don't quite