2015-12-02 104 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