Ios 函数是否被多次调用?
每次歌曲更改时,我都会尝试将歌曲标题和艺术家添加到数据库中。程序目前正在这样做,但由于某种原因,当我转到下一首歌时,同一首歌在数据库中添加了不止一次,即,在解析中添加了6次歌曲1。为什么会发生这种情况?我如何修复它Ios 函数是否被多次调用?,ios,swift,parse-platform,Ios,Swift,Parse Platform,每次歌曲更改时,我都会尝试将歌曲标题和艺术家添加到数据库中。程序目前正在这样做,但由于某种原因,当我转到下一首歌时,同一首歌在数据库中添加了不止一次,即,在解析中添加了6次歌曲1。为什么会发生这种情况?我如何修复它 func applicationDidEnterBackground(application: UIApplication) { print("entered background") NSNotificationCenter.defaultCenter()
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)
}
我注意到的一点是,每次调用
applicationidenterbackground
时,您都会为mpmusicplayercontrollernowplayingitemsdidchangennotification
添加一个观察者
您没有包括
applicationidenterforeground
函数,但希望您正在删除这些函数。如果不删除观察者,将导致为每个ApplicationIdentinterBackground
调用getNowPlayingItem
选择器。因此6applicationidentinterbackground
=6getNowPlayingItem
我的猜测是:您每次添加将调用getNowPlayingItem
的观察者,但不删除它。所以当applicationIDBecomeActive:
移除观测者时。哈哈,你赢了我!我更新了我的代码,但仍然遇到相同的错误。即使在我重新打开应用程序之前,代码也被添加了多次。