0

I'm using the new facebook ios sdk. I request for friends data using the new function showed below. However, since it is a function with a block as a parameter I lost these data outside the function. How can I preserve the data (i.e. store in a global variable) so that I can use it in another function?

Thanks in advance.

code:

-(void)requestFriends {
  [FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection* connection, id data, NSError *error) {
    if(error) {
    [self printError:@"Error requesting /me/friends" error:error];
    return;
  }
  NSArray* friends = (NSArray*)[data data];
}];

1 Answers1

1

Just store it on a property, and refresh the UI after that.

// in .h or class extension
@property(nonatomic, strong) NSArray *friends;

-(void)requestFriends {
    [FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection* connection, id data, NSError *error) {
      if(error) {
      [self printError:@"Error requesting /me/friends" error:error];
      return;
    }
    self.friends = (NSArray*)[data data];
}];
Marcelo
  • 9,916
  • 3
  • 43
  • 52
  • Like Marcelo said you can still access instance variables inside of blocks. You could also call a delegate method or post a notification with the returned data – JLoewy Mar 10 '13 at 09:47
  • I used that method initially but the property can not be stored outside the block. Finally I tried the delegate method and it works. Thanks guys. – yoghourtpuppy Mar 10 '13 at 20:33