Swift中的核心数据:只保存for循环中的最后一个对象

我试图将Core Data中的多个对象保存到for循环中的IPodSongs实体,即for song in result{}当前歌曲的标题。 但是我的代码只保存了循环中最后一首歌曲,只是保持覆盖同一个对象。 而不是覆盖相同的对象,我需要每次创build一个新的对象。 我究竟做错了什么?

 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) } } 

您不需要创build将新对象保存到Core Data的请求。 你做错了什么是创build一个单一的pipe理对象,插入它,然后改变它通过循环,而不是为每首歌创build一个新的对象。

这应该工作:

 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")}在最后。

您在所有迭代中使用相同的插入对象:

 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") 

试试这个,这将工作….

插入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) } 

这将最有可能为你工作! 尝试一下!

 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) } }