2
- (void)viewDidLoad
{
    [super viewDidLoad];
    [VenueManager searchNear:@"Orlando"
                      onLoad:^(NSArray *objects) {
                          self.locationObjects = objects;
                          [self.tableView reloadData];
                   } onError:^(NSError *error) {
                      NSLog(@"%@", error);
    }];

}

This code is in my viewDidLoad method of my UITableViewController class. It is the starting point for using RestKit to parse a JSON file from FourSquare. I was pulling my hair out because i couldn't get the objects to show up in my Table View until i put [self.tableView reloadData];. With out that call the app never even hit my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) because after the block was done executing locationObjects would be nil.

Before when I was debugging it the self.locationsObjects = objects worked when i was in the block (i am very unfamiliar with blocks by the way). As soon as i was out of the block the debugger would say locationObjects was nil, even though it had said it had 30 objects just like objects did when i had a break point at the assignment statement.

Can some one help me understand what is going on here.

Additional info:

Right now everything is working, or appears to be working my table is populated with the objects request from the JSON document. Originally I was doing this exact same thing in a normal ViewController and trying to set the objects from the block equal to locationObjects. Then using a prepareForSegue method i was trying to pass the locationObjects to the tableViewController in the standard method i have learned from numerous tutorials. I would get a SIGBAT error. The thread would terminate because of an unrecognized selector sent to the table view controller. Through debugging i would find that locationObjects could be nil in the prepareForSegue method. Here is the code from the viewController file.

Also I would get a warning here locationTableViewController.locationObjects = self.locationObjects; saying something about assigning a pointer of type NSArray to strong NSArray, or something like that ( i have since changed a lot attempting to get the code working and deleted some storyboard assets, so i'm not 100% sure of the wording).

@implementation CoffeeShopViewController

@synthesize venueCountLable = _venueCountLable;
@synthesize locationObjects = _locationObjects;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [VenueManager searchNear:@"Orlando"
                      onLoad:^(NSArray *objects) {
                          self.venueCountLable.text = [NSString stringWithFormat:@"%d", objects.count];
                         self.locationObjects = objects;
    } onError:^(NSError *error) {
        NSLog(@"%@", error);
    }];

}

- (void)viewDidUnload
{
    [self setVenueCountLable:nil];
    [super viewDidUnload];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"locationTableSegue"])
    {
        LocationTableViewController *locationTableViewController = segue.destinationViewController;
        locationTableViewController.locationObjects = self.locationObjects;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end
Per Larsen
  • 1,149
  • 3
  • 12
  • 25
  • can you show us the property declaration for locationObjects? – yfrancis Nov 05 '12 at 02:39
  • '@property (strong, nonatomic) NSArray *locationObjects;' – Per Larsen Nov 05 '12 at 02:52
  • Can you clarify, is something not working? – danh Nov 05 '12 at 02:54
  • so you say you can see the 30 objects on assignment, but "outside the block" there's nothing. What do you mean outside the block? Where exactly? – yfrancis Nov 05 '12 at 02:57
  • `self.locationObjects = objects;` that line in the 'searchNear' method in the `viewDidLoad` method. `objects would have 30 objects in debugger, the same for `loadObjects right after. When i would debug the `prepareForSegueMethod at ` locationTableViewController.locationObjects = self.locationObjects; ` `self.locationObjects` would be nil – Per Larsen Nov 05 '12 at 03:16

1 Answers1

1

Try:

 @property (nonatomic, copy) NSArray *locationObjects;' 

Edit on why this works:

To be honest, I really don't know the underlying reason for this to work and strong not working. When I saw the problem, it appeared to me that strong being equivalent of retain - inserting copy instead of strong could secure that locationObjects wouldn't be nullified. Thinking again over it, I suspected that my assumption could be wrong - retain literally meant 'Do not release this object because now there is one more guy holding it.'

That, however, works somewhat differently. See this.

What Malaxeur's answer and comments below tells could possibly apply to NSArray in your example - despite strong ownership to locationObjects, what you are given is a reference to objects NSArray (an NSMutableArray*) instead of copy of it. Once out of scope (block end), it is no longer usable, and ARC claims it. Using copy in turn forces it to create another space in memory just for locationObjects, which would remain forever until you free it up.

I still do not consider this a perfect explanation as I have never understood blocks fully. I would keep this open to everyone who knows better, would fill up as soon as I get something that's useful.

Community
  • 1
  • 1
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • that worked. I had to reset everything up so i could create it again but it worked just fine. Amazing that is was something so simple. Could you please explain the reason for this. I know that my problems stemmed from my array being nil. – Per Larsen Nov 05 '12 at 03:45
  • If this works, then `VenueManager` actually provides an `NSMutableArray`, which it then clears. – yfrancis Nov 05 '12 at 03:50
  • `#import #import typedef void (^VenueManagerDidLoadVenuesBlock) (NSArray *venues); @interface VenueManager : NSObject + (void)searchNear:(NSString *)location onLoad:(VenueManagerDidLoadVenuesBlock) loadBlock onError: (RKRequestDidFailLoadWithErrorBlock)errorBlock; @end ` there is my venueManage.h file. I'm guessing it is the NSArray *venue that is in question here. – Per Larsen Nov 05 '12 at 04:06
  • This answer does not make sense, and does not explain how `_locationObjects` could possibly become `nil`. – newacct Nov 05 '12 at 19:27
  • @yfrancis: if that were the case, then our variable would still be pointing to an empty mutable array object, not `nil`. Unless the OP is describing it incorrectly – newacct Nov 05 '12 at 19:28
  • @newacct if Gregory House were here he would say: Rule #1 of SO: OP always lies :P – yfrancis Nov 05 '12 at 20:04
  • if I put a break point at `self.locationObjects = objects;` in my coffeeShopViewController file and then move to the next line of code. Both the variables have a value of "30 objects" in the debugger window. Now if i break point at `locationTableViewController.locationObjects = self.locationObjects;` in my prepare for segue method and move far enough a head to view `locationObjects` in the debugger it is "nil". This method is called when i push a button after my view has loaded and after the first mentioned break point has been hit. – Per Larsen Nov 06 '12 at 00:38