为什么要重复标题?

时间:2015-12-01 00:29:04

标签: ios swift parse-platform

我正在播放当前播放的歌曲,并捕获标题和艺术家,并将其存储在解析中。出于某种原因,当歌曲播放时,该节目增加4个相同的标题/艺术家。我反而只想要一个。我该如何解决这个问题?

my objects as seen in the parse data browser

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated);

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

func getNowPlayingItem() {

    NSNotificationCenter.defaultCenter().removeObserver(self)

    if  let nowPlaying = musicPlayer.nowPlayingItem  {
        let title = nowPlaying[MPMediaItemPropertyTitle] as? String
        let artisttest = nowPlaying[MPMediaItemPropertyTitle]
        if let artist = nowPlaying[MPMediaItemPropertyArtist] as? String{
            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)!)
                    // print(self.Parsearray)
                }

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

                    objectPointer.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if(error != nil){
                            print(error)
                        }else{
                            NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                        }
                    })
                }
            })
        }else{

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

                    // print(self.Parsearray)
                }

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

                    objectPointer.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in

                        if(error != nil){
                            print(error)
                        }else{
                            NSNotificationCenter.defaultCenter().addObserver(self, selector: "getNowPlayingItem", name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification, object: nil)
                        }
                    })
                }
            })
        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据证据,很有可能快速调用函数getNowPlayingItem几次。它会启动查询,其中一些查询在保存之前完成。那些查询完成(尚未完成保存)启动了一些保存并且您获得了少量对象。

通过在方法开始时和saveInBackground注意控制台上的时间戳之前打印消息来检查这一点。

如果我是对的,那么修复很简单:(a)找出为什么要多次调用该方法并修复它,或者(b)将一个布尔实例变量添加到封闭类中,调用它像busySaving这样的东西。在方法开始时,如果busySaving为真,则纾困,否则将其设置为真随身携带。将您的saveInBackground()更改为saveInBackgroundWithBlock()并重置完成块中的busySaving标记。

修改 现在我们看到为什么要重复调用它:因为重复接收通知。修复的一种方法(上面的想法(a))将是在getNowPlayingItem开始时停止观察该通知(NSNotificationCenter removeObserver)。然后,由于您希望获得后续通知,请在保存后使用saveInBackgroundWithBlock重新添加自己作为观察者。请注意,这与saveInBackground see here for reference不同。

如果您愿意,上述构思(b)仍然适用。