heyred
heyred

Reputation: 2051

Xcode unable to retrieve Object

I have an Xcode app that needs to read information from Parse.com.

From my viewDidLoad I call

[self initializeContent];

Which calls:

- (void) initializeContent
{
    [self queryParse: @"MyClassName"];
}

- (void) queryParse:(NSString *) className
{
    NSLog(@"Class Name Is: %@", className);

    PFQuery *query = [PFQuery queryWithClassName:className];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        if (!error)
        {
            myObjectsArray = [[NSArray alloc] initWithArray:objects];
            NSLog(@"Objects Array: %@", myObjectsArray);
        }
        else
        {
            NSLog(@"Error retrieving objects: %@ %@", error, [error userInfo]);
        }

    }];
}

This returns the Object from Parse.

However, the following does not return anything from Parse and the method gets called twice as can be seen from the stack trace

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    PFObject *tempObject = [myObjectsArray objectAtIndex:indexPath.row];
    NSLog(@"Temp Object: %@", tempObject);

    cell.textLabel.text = [tempObject objectForKey:@"MyClassName"];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Does anyone know why the method returns NULL and why it gets called twice in the cellForRowAtIndexPath method?

enter image description here

Upvotes: 0

Views: 31

Answers (1)

quellish
quellish

Reputation: 21254

NSLog(@"Temp Object: %@", tempObject); will invoke description on the tempObject. That method is returning the string "(null)", but the object itself isn't nil - an NSArray can't contain nil items. It COULD be the NSNull value object, which may be because the Parse API is returning that for your request.

It's not likely that NSLog(@"Temp Object: %@", tempObject); is being called twice inside cellForRowAtIndexPath:, but it is likely that cellForRowAtIndexPath: is being called twice - because the data source says that there are two items (tableView:numberOfRowsInSection: is returning 2).

Upvotes: 1

Related Questions