Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

ios - Core Data in Swift: Only saving last object in a for loop

I'm trying to save multiple objects in Core Data to the IPodSongs entity in a for loop, namely the title of the song currently in the for song in result{} loop. But my code only saves the very last song in the loop, and just keeps overwriting the same object. Instead of overwriting the same object, I need to create a new object each time. What am I doing wrong?

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)

            }
        }    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You dont need to create a request to save new objects to Core Data. What you did wrong was create a single managed object, inserted it and then changeded it through the loop instead of creating a new object for each song.

This should work:

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

}

Also put if context.save(&errorPointer == false {printlin("Error received while saving")} at the very end.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...