0

So I'm trying to pull in data from Parse.com and then add it to a global array to update a table view with. Right now I have:

- (void)loadData {
    PFQuery *query = [PFQuery queryWithClassName:@"Event"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (PFObject *object in objects) {
            EventObject *thisEvent = [[EventObject alloc] initWithPFObj:object];
            [self.events addObject:thisEvent];
        }
        [self.tableView reloadData];
    }];
}

When the tableview tries to reload the data, it finds an object in self.events, but the object's properties are all nil. (I think this has something to do with weak/strong self in an asynchronous block, but I can't figure it out.) How do I get the data to be preserved between this block and the reload?

  • Side note: Parse supports NSObject subclasses for models, why don't you use them? I have a feeling that your EventObject has some bug and it does not work correctly with received data. – pronebird Dec 07 '14 at 23:51
  • @Andy I put a break point in the block to check if the Event object is initiated correctly and it is. It has all of the correct data when added to self.events and then is nil-ed out when I try to reload the table. – reco189 Dec 08 '14 at 20:01

1 Answers1

0

Since you are using block to fill data on your array by accessing Self, you must use a weakSelf variable to dont loose the reference.

So you must use this weakSelf instead of just Self, on your loadData method:

__weak typeof(self) weakSelf = self;

Also, remember to put your NSMutableArray "events" as Strong, not Weak.

Edit: A brief explanation of the difference between Strong and Weak references: https://stackoverflow.com/a/18344946/2788711

Community
  • 1
  • 1
Caio
  • 371
  • 5
  • 11