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 trial

iOS

Benjamin Bell
Benjamin Bell
4,364 Points

Why save the PFFile, image or video to Parse.com when this is also done through the PFObject object when it is saved?

When we initialise the PFObject and set one of the objects to the PFFile instance; when saving to Parse.com does that not upload the required image or video data within the PFObject?

- (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 andToHeight:480.0f];
        fileData = UIImagePNGRepresentation(newImage);
        fileName = @"image.png";
        fileType = @"image";
    } else {
        fileData = [NSData dataWithContentsOfFile:self.videoFilePath];
        fileName = @"video.mov";
        fileType = @"video";
    }

    //Preparing file to upload to Parse.com
    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"];
            // We can pass any type, Parse just stores it for us
            [message setObject:file forKey:@"file"];
            [message setObject:fileType forKey:@"fileType"];
            [message setObject:self.recipients forKey:@"recipientIds"];
            // We would normally only want to store the user id and get the username from the user collection but retrieving both will simplify things
            [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
            [message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
            [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 {
                    // Upload was successful we can reset all the parameters ready for a new set
                    [self reset];
                }
            }];

//         }
//    }];
}

I commented out the saveInBackgroundMethod for the PFFile and when I checked on Parse.com, the image is available in the PFObject class Id created.

1 Answer

Stone Preston
Stone Preston
42,016 Points

PFFiles have to be saved to the backend first before they can be associated to a PFObject.Thats why you save the file first, then if the upload is the file is successful, then you associate it with your message object

Benjamin Bell
Benjamin Bell
4,364 Points

I have looked at the Parse documentation and they use the following example for uploading images:

NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];

PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
userPhoto[@"imageName"] = @"My trip to Hawaii!";
userPhoto[@"imageFile"] = imageFile;
[userPhoto saveInBackground];
Stone Preston
Stone Preston
42,016 Points

It's possible they changed the sdk and you no longer have to do that then. Try it and see if it works. The course is pretty old so it's possible they updated the sdk between now and when the video was made.