HalesEnchanted
HalesEnchanted

Reputation: 665

NSInvalidArgumentException: App crashes upon opening

I am trying to build an app where users can like and comment on posts, a lot like instagram. All the posts / likes / comments are saved on parse.com! I have been able to put together the code, which should work as it does on the sample app but when I move over the files, this code makes it crash:

 [query whereKey:@"user" containedIn:self.usersToDisplay];

The crash message being:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* setObjectForKey: object cannot be nil (key: $in)'**

The rest of the code goes something like this:

- (void)viewDidLoad
{
// self.usersToDisplay = @[[PFUser currentUser]];

self.parseClassName = @"Photo";
[super viewDidLoad];

self.tableView.layer.cornerRadius = 10;
self.tableView.layer.masksToBounds = YES;
}

- (void)setUsersToDisplay:(NSArray *)usersToDisplay {
_usersToDisplay = usersToDisplay;

if (self.isViewLoaded)
{
    [self loadObjects];
}
}


  - (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
    query.cachePolicy = kPFCachePolicyNetworkOnly;
}

 [query whereKey:@"user" containedIn:self.usersToDisplay];
[query addDescendingOrder:@"createdAt"];
return query;
}

 -(PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  {
   PhotoViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCellReuseID"];
if(!cell)
{
    cell = [[PhotoViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ImageCellReuseID"];
}

cell.imageView.file = object[@"image"];
[cell.imageView loadInBackground];

NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSString* uploaded = [formatter stringFromDate:object.createdAt];

cell.textLabel.text = [NSString stringWithFormat:@"Taken By %@ ", object[@"username"]];

cell.detailTextLabel.text = uploaded;

return cell;
}


  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [self performSegueWithIdentifier:@"ShowDetail" sender:indexPath];
 }

Any idea why it crashes?

Upvotes: 1

Views: 73

Answers (1)

Rob Norback
Rob Norback

Reputation: 6599

It looks like one of the values in your array self.usersToDisplay doesn't have a valid object for the query.

From the parse documentation:

If you want to retrieve objects matching several different values, you can use whereKey:containedIn:, providing an array of acceptable values. This is often useful to replace multiple queries with a single query. For example, if you want to retrieve scores made by any player in a particular list:

// Finds scores from any of Jonathan, Dario, or Shawn
// Using PFQuery
NSArray *names = @[@"Jonathan Walsh", @"Dario Wunsch", @"Shawn Simon"]; 
[query whereKey:@"playerName" containedIn:names];

Make sure all the values in your array match the values in your database.

Cheers!

Upvotes: 1

Related Questions