Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

robert cioffi
robert cioffi
3,466 Points

[ ] empty recipients array Ribbit

I'm getting an empty recipients array on Parse.com with the Ribbit app, I've looked at the code from teamtreehouse and compared to mine and it looks the same, does anyone know why the recipients won't upload?

My Camera View code:

#import "CameraViewController.h" 
#import <MobileCoreServices/UTCoreTypes.h>

@interface CameraViewController ()

@end

@implementation CameraViewController


- (void)viewDidLoad
{
[super viewDidLoad];
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];  
   self.recipients = [[NSMutableArray alloc] init];  
}

-(void) viewWillAppear:(BOOL)animated   {  //this will appear is where loaded items loop back to normally
[super viewWillAppear:animated];
   PFQuery *query = [self.friendsRelation query];  //grabs relations to current user
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if(error){
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {
        self.friends = objects;
        [self.tableView reloadData];
    }

}];  

if (self.image == nil && [self.videoFilePath length] == 0){
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.videoMaximumDuration = 10; 

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])     {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } // if  camera available
    else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    } // if no camera available like in simulator

    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

    [self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.friends count];
}


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

PFUser *user = [self.friends objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;

NSLog(@"cell4indx self.receipients: %@", self.recipients);// this is where my problem is, the NSLog prints out ( ) meaning there aren't any recipients

if ([self.recipients containsObject:user.objectId]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;
}

#pragma mark - Image Picker Controller Delegate

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker    {
[self dismissViewControllerAnimated:NO completion:nil]; 
[self.tabBarController setSelectedIndex:0]; 
}


-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    //A photo was taken/selected
    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //save the image!
        UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
        //dismiss camera view
}
else {
        // A video was taken or selected
        self.videoFilePath = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path];
        if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //save the video!
                if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
        }
        }
    [self dismissViewControllerAnimated:YES completion:nil];  //close camera

    }

}

#pragma mark - Table View Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];


if(cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.recipients addObject:user.objectId];
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
    [self.recipients removeObject:user.objectId];
}
NSLog(@"didSelect self.receipients: %@", self.recipients);
}

pragma mark - IBActions

  • (IBAction)send:(id)sender {

    if(self.image == nil && self.videoFilePath.length == 0) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Try Again!" message:@"Please capture or select a photo or video to share!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; [self presentViewController:self.imagePicker animated:NO completion:nil]; } else { [self uploadMessage]; // removed reset because of bug, put below [self.tabBarController setSelectedIndex:0]; //goes to inbox } }

    • (IBAction)cancel:(id)sender { [self reset];

    [self.tabBarController setSelectedIndex:0]; //goes to inbox

    }

    pragma mark - Helper Methods

    • (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:480.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:@"An error Occurred!" message:@"Please try sending your message again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else {

        PFObject *message = [PFObject objectWithClassName:@"Messages"];
        message[@"file"] = file;
        message[@"fileType"] = fileType;
        message[@"recipientsIds"] = self.recipients;
        NSLog(@"the receipients are: %@", self.recipients);  //NULL but why?
        message[@"senderId"] = [[PFUser currentUser]objectId];
        message[@"senderName"] = [[PFUser currentUser]username];
    
       [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error Occurred!" message:@"Please try sending your message again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
    
            } else {
                    //everything was successful!
                 [self reset];
                }
        }];
    

    } }];

    // IF image, shrink it // upload the file itself // upload the message details }

    • (void)reset { self.image = nil; self.videoFilePath = nil; [self.recipients removeAllObjects]; }
    • (UIImage *) resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height {

    CGSize newSize = CGSizeMake(width, height); CGRect newRectangle = CGRectMake(0, 0, width, height); UIGraphicsBeginImageContext(newSize); [self.image drawInRect:newRectangle]; UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

    return resizedImage; }

    @end

1 Answer

Are your code commands using exactly the column names in Parse? I saw one possible error with receipients in a @" text parameter.

I can't go through all thr code but if your code and Parse aren't talking, then that's a starting point.

Steve.