2

I just learned how to store an array into a Parse Cloud using the example provided by the Parse Documentation:

gameScore.addUniqueObjectsFromArray(["flying", "kungfu"], forKey:"skills")
gameScore.saveInBackground()

Now, utilizing this logic, I want to append strings into the array. So this is what I wrote:

@IBAction func requestButtonPressed(sender: AnyObject) {
    var prayerRequests = PFObject(className: "PrayerRequests")
    prayerRequests.addObject(["YOIDJFO"], forKey:"skills")
    prayerRequests.saveInBackground()
}

Now, after having executed the function requestButtonPressed three times, in parse this is happening:

Parse Array

However. I don't want that to happen when I execute the function requestButtonPressed three times. I want it to be something like this:

enter image description here

Anybody have a solution to this problem?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jae Kim
  • 625
  • 3
  • 12
  • 23

2 Answers2

2

Every time you use this statement var prayerRequests = PFObject(className: "PrayerRequests") a new PFObject will be created. In order to update a object you need to query the object first and then update its field. In your case you should first get the array by querying for the object, modify / append data to the array and then update the object.

Santhosh
  • 691
  • 4
  • 12
  • That was one solution that I was thinking of but then wouldn't that mean I would have to create a new object every time I want to modify the array? That would mean I would have many unnecessary objects? Maybe? – Jae Kim Sep 10 '15 at 06:25
0

Instead of doing addObject, do insertObject:{yourObject} atIndexPath:{storingPosition} forKey:{@"youKey"}.

And the the value you are adding is an array ["YOIDJFO"] , object should be like {"YOIDJFO"}

Vikas Mishra
  • 246
  • 1
  • 12
  • Thank you so much for the response. Could you please translate the syntax into swift code? I tried it and I'm getting some syntax errors... – Jae Kim Sep 10 '15 at 06:24