[英]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). 我将Podcasts及其相关情节保存到Parse,但是以下代码仅保存1集和Podcast(我想在for循环中找到的每个条目都会重置
currentP
并且仅保存找到的最后一个值)。
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: 如果我使用诸如
episodesToParse.addObject(episode.date, forKey: "showDate")
类的东西episodesToParse.addObject(episode.date, forKey: "showDate")
则会返回以下错误:
[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).
我想要的是将currentP原样保存,并将其所有情节保存到与其父级有关系的另一个类中(播客)。 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
episodesToParse
at once. 我发现吨的方法可以做到这一点,如果您要添加一次一个小插曲,但没有一大堆人(我想能够保存的500例
episodesToParse
一次。
Thanks for your help. 谢谢你的帮助。
Your problem is, that you save the episodesToParse
after the loop. 您的问题是,在循环后保存了
episodesToParse
。 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: 您必须在循环内移动
episodesToParse.saveInBackground()
,以便每次循环设置该集的属性时,都会更新该集:
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: 或者,您可以使用
PFObject.saveAllInBackground
保存所有对象:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.