ios - Parse relations in Swift -


i'm in process of making app creates events. event has property user saves successfully, want save array of events current user. each time event created i'm trying add event array (for key "events" in user class).

what's best way go in swift? here's sample code of save event parse.

the first gets image file , passes function save whole event...

   @ibaction func saveevent(sender: anyobject) {      let picturedata = uiimagepngrepresentation(eventimage.image)      let file = pffile(name: "eventimage", data: picturedata)      file.saveinbackgroundwithblock { (succeeded, error) -> void in          if !(error != nil) {             self.saveeventtoparse(file)         }          else if let error = error {             println("error: \(error.localizeddescription)")         }      }   } 

this function saves event

func saveeventtoparse(file: pffile) {      let event = event(image: file, user: pfuser.currentuser()!, comment: eventdescriptionfield.text, title: eventtitlefield.text, date: datepicker.date)      event.saveinbackgroundwithblock { (success, error) -> void in         if success {              //i know need here that's not              event.user.relationforkey("events")               var alertview = uialertview(title: "event saved!", message: "success", delegate: nil, cancelbuttontitle: "okay!")             alertview.show()         }         else {             println("error: \(error?.localizeddescription)")         }      } } 

thank you!

in parse, relation used "many-to-many" relationship.

in case, i'm not sure statement "one event can belong many user" right or wrong. however, can continue using relation this:

let relation = event.user.relationforkey("events") relation.addobject(event) event.user.saveinbackground() 

more information can refer here: https://parse.com/docs/ios/guide#relations-using-parse-relations


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -