applicationDidEnterBackground

applicationDidEnterBackground

This question already has an answer here:
Why is the title being repeated?
                                
                                    (1个答案)
                                
                        
                                4年前关闭。
            
                    
每当歌曲更改时,我都试图将歌曲标题和歌手添加到数据库中。该程序当前正在执行此操作,但是由于某种原因,当我转到下一首歌曲时,同一首歌曲在数据库中被添加了一次以上,即在分析中歌曲1被添加了6次。为什么会发生这种情况,我该如何解决?

   func applicationDidEnterBackground(application: UIApplication) {
    print("entered background")


    NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
    musicPlayer.beginGeneratingPlaybackNotifications()

    backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in

        UIApplication.sharedApplication().endBackgroundTask(self.backgroundTask!)
        self.backgroundTask = UIBackgroundTaskInvalid
    })


}



func getNowPlayingItem() {
    if  let nowPlaying = musicPlayer.nowPlayingItem  {
        let title = nowPlaying[MPMediaItemPropertyTitle]
        let artisttest = nowPlaying[MPMediaItemPropertyTitle]
        if let artist = nowPlaying[MPMediaItemPropertyArtist] {

            let objectPointer = PFObject(className: "Pointer")
            let object = PFObject(className: "MasterSongs")



            let query = PFQuery(className: "Pointer")
            query.findObjectsInBackgroundWithBlock({
                (objects: [AnyObject]?, error: NSError?) -> Void in
                var objectIDs = objects as! [PFObject]

                for i in 0...objectIDs.count-1{
                    self.Parsearray.append((objectIDs[i].valueForKey("title") as? String)!)


                }


                if self.Parsearray.contains(title! as! String){
                    print("already in db")
                }else{


                 objectPointer["title"] = title
                    objectPointer["user"] = PFUser.currentUser()
                    objectPointer["artist"] = artist
                    objectPointer.saveInBackground()
                    //parseClass.saveInBackgroundWithBlock{(success: Bool, error: NSError!) -> Void in
                    objectPointer.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if success == false {
                            print(error)
                        } else {
                            print("Posted succesfully")
                        }
                    })

                }

            })




        }

            if(artisttest == nil){
                let objectPointer = PFObject(className: "Pointer")


                let query = PFQuery(className: "Pointer")
                query.findObjectsInBackgroundWithBlock({
                    (objects: [AnyObject]?, error: NSError?) -> Void in
                    var objectIDs = objects as! [PFObject]

                    for i in 0...objectIDs.count-1{
                        self.Parsearray.append((objectIDs[i].valueForKey("title") as? String)!)

                    }


                    if self.Parsearray.contains(title! as! String){
                        print("already in db")
                    }else{
                        objectPointer["title"] = title
                        objectPointer["user"] = PFUser.currentUser()
                        objectPointer["artist"] = "No artist found :("
                        objectPointer.saveInBackground()


                    }

                })





            }


        }




}
 func applicationWillEnterForeground(application: UIApplication) {

        NSNotificationCenter.defaultCenter().removeObserver(self)

}

最佳答案

我注意到的一件事是,每次调用MPMusicPlayerControllerNowPlayingItemDidChangeNotification时,都要为applicationDidEnterBackground添加一个观察者。

您没有包含applicationDidEnterForeground函数-但希望您在那里删除了那些观察者。如果不删除观察者,则会导致为每个getNowPlayingItem调用applicationDidEnterBackground选择器。因此6 applicationDidEnterBackground = 6 getNowPlayingItem

10-08 07:27