Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMcgerald Lezeau
7,096 PointsProblem retrieving messages
i did everything i was suppose to do typed in the code and rechecked the code using the project file everything looks perfect there are no errors and for some reason i still can't retrieve messages
11 Answers
Patrick Donahue
9,523 PointsCan you show us the code from the send message and receive message methods please? Also make sure the names of the database columns are the same. I ran into the issue with a misspelling.
Mcgerald Lezeau
7,096 Points-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];
// parse.com retreiving data
PFQuery *query =[PFQuery queryWithClassName:@"Messages"];
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser]objectId]];
[query orderByDescending:@"createdAt"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(!error){ NSLog(@"Error: %@ %@ ", error , [error userInfo]); }else{ // There are messages self.messages = objects; [self.tableView reloadData];
NSLog(@"Retrieved %lu messages",[self.messages count]);
}
}];
}
Patrick Donahue
9,523 PointsYou have "recipientIDS and recipientsIDs"
[message setObject:self.recipients forKey:@"recipientsIds"];
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser]objectId]];
Change them so they match.
Mcgerald Lezeau
7,096 PointsIve done that a its still not reserving messages
Patrick Donahue
9,523 PointsDid you check parse.com to make sure your messages are getting there?
If they are you can see who the messages are being sent to and that they are uploading correctly. We can then go into the inbox file and see what is wrong in there.
Also, make sure the column headers in parse match the keys in your code.
Mcgerald Lezeau
7,096 Pointsyes they are getting there i just can't retrieve them
Patrick Donahue
9,523 PointsCan you show me your viewDidLoad and cellForRowAtIndexPath methods from the InboxViewController.m file
Mcgerald Lezeau
7,096 Points-
(void)viewDidLoad { [super viewDidLoad];
// if current user then do not show login page else go to login page PFUser *currentUser = [PFUser currentUser]; if (currentUser) { NSLog(@"Current user: %@", currentUser.username);
// do stuff with the user
} else { [self performSegueWithIdentifier:@"showLogin" sender:self];
// show the signup or login screen
} [self performSegueWithIdentifier:@"showLogin" sender:self]; }
// to automatically load updated information
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];
// parse.com retreiving data
PFQuery *query =[PFQuery queryWithClassName:@"Messages"];
[query whereKey:@"recipientsIds"equalTo:[[PFUser currentUser]objectId]];
[query orderByDescending:@"createdAt"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(!error){ NSLog(@"Error: %@ %@ ", error , [error userInfo]); }else{ // There are messages self.messages = objects; [self.tableView reloadData];
NSLog(@"Retrieved %lu messages",[self.messages count]);
}
}];
}
Mcgerald Lezeau
7,096 Points-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell!";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"senderName"];
NSString *fileType = [message objectForKey:@"fileType"];
if ([fileType isEqualToString:@"image"]) {
cell.imageView.image = [UIImage imageNamed:@"icon_image"];
}
else {
cell.imageView.image = [UIImage imageNamed:@"icon_video"];
}
return cell;
}
Patrick Donahue
9,523 PointsSee if your CellIdentifier Cell! is the same as the one in the storyboard.
Or just remove the exclamation point. See if that works.
Mcgerald Lezeau
7,096 Pointsoh i purposely changed the cell identifier to Cell! so it wouldn't get confused with another cell i identifier i had on another viewtable
Patrick Donahue
9,523 PointsI had another idea - did you add the viewController class to the view in the storyboard?
Mcgerald Lezeau
7,096 Points2014-04-09 20:48:05.404 Social[38376:60b] Error: (null) (null) this is my error message every time i click on the view controller tab it basically can't find any messages i check my string names and nothing is pulling through from the backend
Mcgerald Lezeau
7,096 PointsMy Good Friend i found the error everything was correct except for my error message smh it took me three days to see such a simple error.
Patrick Donahue
9,523 PointsBeen there! I hate when that happens.
Mcgerald Lezeau
7,096 Points-
(void) uploadMessage{ NSData *fileData; NSString *fileName; NSString *fileType;
// Check if image or video if (self.image != nil) { UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:568.0f]; fileData = UIImagePNGRepresentation(newImage); fileName = @"image.png"; fileType = @"image";
}else{ fileData = [NSData dataWithContentsOfFile:self.videoFilePath]; fileName = @"video.mov"; fileType = @"video"; } PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Opps!" message:@"Please try sending message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alertView show];
}else{ // message is a object to send back to parse.com PFObject *message = [PFObject objectWithClassName:@"Messages"]; // the message will be identified as a file , the filetype , recipients id's , // the senders id will be recorded alond with his usernamename [message setObject:file forKey: @"file"]; [message setObject:fileType forKey:@"fileType"]; [message setObject:self.recipients forKey:@"recipientsIds"]; [message setObject:[[PFUser currentUser]objectId] forKey:@"senderId"]; [message setObject:[[PFUser currentUser]username] forKey:@"senderName"]; // then save in background with block [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if(error){ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Opps!" message:@"Please try sending message again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alertView show]; }else{ [self reset]; } } ];} }];