1

I'm trying to load data from API and show it inside a UITableView. I used AFNetworking for the network calls, but now I'm facing a problem: I can't access myTableView nor self inside the success block.

@property (weak, nonatomic) IBOutlet UITableView *myTableView; 
@property (nonatomic,retain) NSMutableArray *myDataSource;

@synthesize featuredProductsTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NetworkManager *networkManager = [NetworkManager  getInstance];
    myDataSource = [NSMutableArray array]; 

    [networkManager getPath:@"example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        [myDataSource addObjectsFromArray:JSON];

        [myTableView reloadData]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

Debugging the code shows that inside the block I can't access any of self, myDataSource, or myTableView.

How can I solve this issue?

Thanks.

trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
  • 4
    To access myDataSource you should use __block myDataSource = [NSMutableArray array]; , self and myTableView must be accessable – B.S. Apr 25 '13 at 14:24
  • variables accessed inside a block without having being declared with __block prefix is a copy of the real variable – Jerome Diaz Apr 25 '13 at 14:26
  • i changed it to @property (nonatomic,retain) __block NSMutableArray *myDataSource; and still can't access any of them – trrrrrrm Apr 25 '13 at 14:35
  • Sorry where it is not accesable? Inside [networkManager getPath ... block ? – B.S. Apr 25 '13 at 14:37
  • when trying to add the JSON to the dataSource and reloading the tableView, debugging shows that self is 0x00000000 and the others are invalid Expressions – trrrrrrm Apr 25 '13 at 14:41
  • @George ya some testing with __block fixed the problem, many thanks may you please add it as an answer so i can mark it. – trrrrrrm Apr 25 '13 at 15:01

2 Answers2

0

You should probably use :

__block myDataSource = [NSMutableArray array];

You can also read:

What does the "__block" keyword mean?

Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109
  • This is completely wrong. First, `__block` can only be applied to local variables. `myDataSource` is an instance variable. Second, even if it were a local variable, `__block` is only useful if the variable is assigned to. Nowhere is the variable assigned to other than when it is initialized before the block. – newacct Apr 25 '13 at 23:46
  • Read the question and comments before downvoting, as you see the question was edited – B.S. Apr 26 '13 at 06:48
  • `myDataSource` was an instance variable in the original version – newacct Apr 26 '13 at 07:03
-2

Declare this right before your block:

__block typeof(self) this = self;

At least, this will allow you to reach self from inside the block, by calling it this.

Edit: that's not the way to go, my mistake. See How do I avoid capturing self in blocks when implementing an API?

Community
  • 1
  • 1
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • This is completely wrong. Of course you can access `self` from inside the block. – newacct Apr 25 '13 at 23:47
  • Then why does the static analyzer complain it's likely to lead to a retain cycle if I use `self` in (some) blocks? So I tried to teach myself and found this: http://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api – Cyrille Apr 26 '13 at 06:46
  • it might lead to a retain cycle, but that does not mean you can't do it – newacct Apr 26 '13 at 07:03