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

Uploading a variable number of images to Parse Backend

Hi all,

My question is regarding using Parse to upload multiple images.

Users will be able to upload any number of images associated to a place they like and save to the backend. My question is what is the best way to do this? I have come up with two possible methods:

Using relationships

Create a "image" class on parse and simply declare a relation between those images and the place

Using Arrays

I have never used arrays in Parse but I assume I could just create a property of type array for class "place" and simply save a bunch of images?

If you have further suggestions, feel free to add!

In my limited knowledge of the full scope of your app, it seems like relationships would be the more efficient, cleaner way of handling it. I guess if you're only ever going to have one relationship type then maybe it doesn't make a difference... but to keep a level of flexibility and freedom to add in additional relationships at a later time (new feature), then relationships is more appealing to me.

Like it would be easier to manipulate relationships on a per photo basis, based on a specified criteria vs. sorting through multiple arrays of images to identify by the same criteria.

Hi Kevin, thanks for your comment.

To give you a better idea of the full scope, you can think of it similar to a place of yelp and instagram. There will be a bunch of places and each place with have photos associated with it. However, the number of photos uploaded for a single place can be varied.

In the Parse documentation, I found reference to 3 possibilities. Note, all three options assume there is a PFObject called place that already exists

Option 1: (using a property)

For a single image, the documentation suggests

PFObject *image = [PFObject objectWithClassName:@"Image"];
image[@"place"] = @"Name of place" 
image[@"imageFile"] = imageFile; //imageFile is simply NSData object of a UIImage
[image saveInBackground]

Based on this, if multiple images had to be uploaded, the code would look something like this:

for (int i = 1, i<=10,i++){
     PFObject *image = [PFObject objectWithClassName:"Image"];
     image[@"place"] = place[@"objectID"]; //Changed to objectID so that it is unique
     image[@"imageFile"] = imageFiles[i];
     [image saveInBackground]
}

Option 2: (using arrays)

NSArray *imagesArray = .... //Array of images
PFObject *places = [PFObject objectWithClassName:@"Places"];
place[@"imagesArray"] = imagesArray;

Option 3: (using relations)

Similar to 1, a for loop with probably have to be used

for (int i = 1, i<=10,i++){
     PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData[i]];
     PFObject *image = [PFObject objectWithClassName:@"Image"];
     image[@"imageFile"] = imageFile

     PFRelation *relation = [place relationForKey:@"images"];

     [relation addObjects:image]
     [place saveInBackground]
}

I'm probably more keen on Option 3 particularly because of this statement from the docs:

"We do not recommend storing large pieces of binary data like images or documents using NSData fields on PFObject. PFObjects should not exceed 128 kilobytes in size. "

Let me know which of the 3 you think is best or if there is another option.

Yeah, by no means am I anything more than a beginner when it comes to iOS development, but just logically thinking I would take option 3 if I was in your position... even more so due to the statement on Parse.