I am getting the current playing song, and capturing the title and artist, and storing it in parse. For some reason, when the song plays, the program adds 4 or so of the same ti
From the evidence, there's a good chance that the function getNowPlayingItem
is being called several times rapidly. It launches queries, a handful of which complete before anything is saved. Those query completions (with no saves done yet) launch a handful of saves and you get a handful of objects.
Check this by printing a message at the start of the method and just before saveInBackground
paying attention to the timestamps on the console.
If I'm right, the fix is simple: (a) find out why the method is being called so many times and fix that, or (b) add a boolean instance variable to the enclosing class, call it something like busySaving
. At the start of the method, bail out if busySaving
is true, otherwise set it to true an carry on. Change your saveInBackground()
to saveInBackgroundWithBlock()
and reset the busySaving
flag in the completion block.
EDIT
Now we see why it's being called repeatedly: because the notification is being received repeatedly. One way to fix (idea (a) above) would be to stop observing that notification (NSNotificationCenter removeObserver) at the start of getNowPlayingItem
. Then, since you want to get subsequent notifications, re-add yourself as an observer after the save, using saveInBackgroundWithBlock
. Notice this is different from saveInBackground
see here for reference.
Idea (b) above still applies as well, if you prefer.