我试图将核心数据中的多个对象保存到for
循环中的IPodSongs实体,即当前位于for song in result{}
循环中的歌曲的标题。但是我的代码只保存循环中的最后一首歌,并且只是保持覆盖同一个对象。我不需要覆盖同一个对象,而是每次都需要创建一个新对象。我做错了什么?
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
var request = NSFetchRequest(entityName: "IPodSongs")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
context.save(nil)
}
}
答案 0 :(得分:3)
您无需创建将新对象保存到Core Data的请求。你做错了是创建一个托管对象,插入它然后通过循环改变它,而不是为每首歌创建一个新对象。
这应该有效:
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
}
}
if context.save(&errorPointer == false {
printlin("Error received while saving")
}
}
同时将if context.save(&errorPointer == false {printlin("Error received while saving")}
放在最后。
答案 1 :(得分:1)
您在所有迭代中使用相同的插入对象:
for song in song.items as! [MPMediaItem] {
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
您确实正在更改歌曲的对象值,但您使用的是同一个实例,而后者又是核心数据库中的同一个实体。
您需要为每首新歌使用新实例:
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
答案 2 :(得分:1)
试试这个,这会有用......
插入for循环并保存在外部循环...
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
context.insert(newSong)
}
}
context.save(nil)
}
答案 3 :(得分:1)
这最有可能对你有用!试试吧!
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
context.save(nil)
}
}