2015-12-02 114 views
0

我试图在歌曲每次改变时将歌曲标题和艺术家添加到数据库中。该节目目前是这样做的,但由于某种原因,当我进入下一首歌曲时,同一首歌曲会在数据库中多次添加,即歌曲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) 

} 
+0

我的猜测:你每次添加将调用'getNowPlayingItem'的观察者,但你不会删除它。所以当'applicationDidBecomeActive:'移除观察者时。 – Larme

+0

哈哈,你打败我吧! –

回答

0

有一两件事我注意到的是,你每天applicationDidEnterBackground被调用时添加一个观察员MPMusicPlayerControllerNowPlayingItemDidChangeNotification

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

+0

我更新了我的代码,但我仍然遇到同样的错误。 – tman0542

+0

甚至在我重新打开应用程序之前,它会被添加多次。 – tman0542