Kwnstantinos Natsios Kwnstantinos Natsios - 1 year ago 81
iOS Question

Swift 2 save multiple images to Parse

I have an array of images and i would like to upload them in a Class in Parse but in separate lines with a pointer to another class.

So my code structure is like this

First i save an object successfully and inside the

i get the object's object id. Then i want to save those images with Pointer to the object id that i took before inside a column and the files one by one. A code sample

//First object that i save to class "Point"

obj["user"] = PFUser.currentUser()
obj["title"] = "test"
obj.saveInBackgroundWithBlock { (success, error) -> Void in
if error == nil {
let ojId = self.obj.objectId //this is the object id of the already saved object.

//and now im trying to save to another Class (named Gallery) the images with a pointer ojId.

for i in 0...self.myArray.count-1 {
let imageData = self.myArray[i].mediumQualityJPEGNSData
let imageFile = PFFile(name: "image.JPEG", data: imageData)
self.galleryImages["images"] = imageFile
self.galleryImages["point"] = PFObject(outDataWithClassName: "Point", objectId: "\(ojId!)")
self.galleryImages.saveInBackgroundWithBlock { (success, error) -> Void in
if error == nil {
let ojIdG = self.galleryImages.objectId


With the above code it saves ONLY the last image from the array, although it gets inside this as many times as its count.

Any idea of why this is happening?

UPDATE. It doesnt create a new row every time it saves images. It replaces the row with the next image.

Answer Source

As this code overwrites the row you have to specific that you want the parse to create another row, as like this:

   // this is what you add right after the "for"

        let galleryImages = PFObject(className: yourClassName) 

    // then your code will remain the same!

        let imageData = self.myArray[i].mediumQualityJPEGNSData.....

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download