Allen
Allen

Reputation: 3771

How access variables within method block?

I'm trying to access a method block but I have no idea how to:

__block NSString *username;
PFUser *user = [[self.messageData objectAtIndex:indexPath.row] objectForKey:@"author"];
[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    username = [object objectForKey:@"username"]; 
    NSLog(@"%@", username); //returns "bob";
}];
NSLog(@"%@", username); //returns null

How do I access the variable 'username' from this code outside of the block?

Upvotes: 0

Views: 172

Answers (4)

ivan_yorgov
ivan_yorgov

Reputation: 1401

I would suggest using NSOperationQueue as presented in WWDC. See this article for reference, it think it would be helpful: https://stavash.wordpress.com/2012/12/14/advanced-issues-asynchronous-uitableviewcell-content-loading-done-right/

Upvotes: 1

dariaa
dariaa

Reputation: 6385

fetchIfNeededInBackgroundWithBlock is an asynchronous method. That's why your last NSLog returns null because this it is performed before username was retrieved. So what you want is probably to call some method inside the block to be sure that it executes after you fetched your user data. Something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    MyUserCell *userCell = (MyUserCell *)[tableView dequeueReusableCellWithIdentifier:MyUserCellIdentifier];
    PFUser *user = [[self.messageData objectAtIndex:indexPath.row] objectForKey:@"author"];
    userCell.user = user;
    [user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (object == userCell.user && !error) {
            username = [object objectForKey:@"username"]; 
            cell.textLabel.text = userName;
        }
    }]; 
}

UPDATE: The answer is updated to for the case when the block is called inside tableView:cellForRowAtIndexPath: method as requested. NOTE: Here you will probably need a custom cell to store a reference to the current user, because if you are reusing your cells the block callback might be called after the same cell was reused for a different indexPath (so it will have a different user).

Upvotes: 2

Geekoder
Geekoder

Reputation: 1531

Below is the example of what I do: try it:

Write this below import statement

typedef double (^add_block)(double,double);

Block - Write this in view did load

__block int bx=5;
[self exampleMethodWithBlockType:^(double a,double b){
    int ax=2;
    //bx=3;
    bx=1000;
    NSLog(@"AX = %d && BX = %d",ax,bx);
    return a+b; 
}];

NSLog(@"BX = %d",bx);

Method:

-(void)exampleMethodWithBlockType:(add_block)addFunction {
    NSLog(@"Value using block type = %0.2f",addFunction(12.4,7.8));
}

Upvotes: -2

ayon
ayon

Reputation: 2180

Actually you are accessing the variable username outside the block. You are getting null because the block runs in another thread and you set the value after the block finish it's execution. So, your last line has been already executed in main thread while the block was running , so it's value was not set when last line was executed.That's why you are getting null.

Upvotes: 6

Related Questions