user2747220
user2747220

Reputation: 893

One to many relations Parse

I've looked all over but I can't find an answer to this question.

I am saving a Podcasts and its related episodes to Parse but the following code only saves 1 episode and the podcast (I suppose every entry found in the for loop resets currentP and only the last value found gets saved).

 let currentP = PFObject(className: self.podcastClass)
    currentP["user"] = PFUser.currentUser()
    currentP["name"] = name
    currentP["artist"] = artist
    currentP["summary"] = summary
    currentP["feedURL"] = feedURL
    currentP["artworkURL"] = artworkURL
    currentP["artwork"] = artwork
    currentP["date"] = date

 let episodesToParse = PFObject(className: self.episodesClass)
    for episode in episodes {
    episodesToParse["showDate"] = episode.date
    episodesToParse["title"] = episode.title          
    episodesToParse["downloadURL"] =  episode.enclosures[0].valueForKey("url") as? String
    episodesToParse["showNotes"] = episode.summary
    episodesToParse["localPath"] = ""
    episodesToParse["isDownloaded"] = "no"
    episodesToParse["parent"] = currentP
}
 episodesToParse.saveInBackground()

If I use something like episodesToParse.addObject(episode.date, forKey: "showDate") then the following error is returned:

[Error]: invalid type for key showDate, expected date, but got array (Code: 111, Version: 1.8.1)

I'm not sure how to proceed. What I want is currentP to be saved as it is and all its episodes to be saved in a different class with a relationship to its parent (Podcast). I found tons of ways to do this if you're adding one episode at a time but not a whole bunch of them (I would like to be able to save 500 instance of episodesToParseat once.

Thanks for your help.

Upvotes: 0

Views: 52

Answers (1)

Christian
Christian

Reputation: 22343

Your problem is, that you save the episodesToParse after the loop. You have to move the episodesToParse.saveInBackground() inside the loop so that everytime the loop sets the properties of the episode the episode gets updated:

for episode in episodes {
    episodesToParse["showDate"] = episode.date
    episodesToParse["title"] = episode.title          
    episodesToParse["downloadURL"] =  episode.enclosures[0].valueForKey("url") as? String
    episodesToParse["showNotes"] = episode.summary
    episodesToParse["localPath"] = ""
    episodesToParse["isDownloaded"] = "no"
    episodesToParse["parent"] = currentP

    //Inside
    episodesToParse.saveInBackground()
}

Or you could use PFObject.saveAllInBackground to save all objects:

var episodesToSave[PFObject] = []

for episode in episodes {
    var episodeToParse
    episodeToParse["showDate"] = episode.date
    episodeToParse["title"] = episode.title          
    episodeToParse["downloadURL"] =  episode.enclosures[0].valueForKey("url") as? String
    episodeToParse["showNotes"] = episode.summary
    episodeToParse["localPath"] = ""
    episodeToParse["isDownloaded"] = "no"
    episodeToParse["parent"] = currentP

    //Add to episode-array
    episodesToSave.append(episodesToParse)
}

//Save all objects in the array
PFObject.saveAllInBackground(episodesToSave)

Upvotes: 2

Related Questions