How unique is MPMediaItemPropertyPersistentID?
Asked Answered
C

4

9

How unique is MPMediaItemPropertyPersistentID? Will it even work when synching the list of IDs to another device connected to the same iTunes Account?

I want to implement a iCloud synchronized playlist solution which stores the IDs as a list and I need to know if this will be possible.

Cherey answered 30/8, 2012 at 11:8 Comment(0)
T
7

As per the documentation (emphasis mine)...

The value of the MPMediaItemPropertyPersistentID identifier persists across application launches and across syncs that do not change the sync status of the media item. The value is not guaranteed to persist across a sync/unsync/sync cycle.

As such, given that it won't even persist on this basis I'd be surprised if it persisted across devices in a sufficiently robust manner, if at all.

Twobyfour answered 2/9, 2012 at 15:28 Comment(2)
I've missed this part of the documentation. Looks like I'll have to search for another way...Cherey
Whilst a bit clunky, you may have to construct something consistent via the valueForProperty data available for each MPMediaEntity. (i.e.: The MPMediaItemPropertyTitle, MPMediaItemPropertyAlbumTitle, MPMediaItemPropertyArtist, etc.)Twobyfour
F
4

In my understanding this is not possible. I think this ID is persistent only for each device. It is no unique identifier for a specific song in the iTunes Store. It is only a ID for your own synced songs.

As you read the documentation, you'll see how fragile this ID could be.

"The value is not guaranteed to persist across a sync/unsync/sync cycle."

So if you sync your song database with iTunes and maybe delete a song from your iOS device and the sync again and put it back to your device, you may not get the same ID again for this song. And for sure not across other devices.

So I think, what you're trying to do will not work, until you get a worldwide identical Identifier for each song of the iTunes catalogue or your own iTunes catalogue on the Mac (where the Mac has to handle the IDs).

Fein answered 2/9, 2012 at 15:32 Comment(0)
L
2

The other answers are a little big vague, so here is an answer from my own experiences and tests:

1) You can't use MPMediaItemPropertyPersistentID to get an ID that is equal between devices.
2) The MPMediaItemPropertyPersistentID will change when the device is synced with another iTunes library or all music is removed from the device and then synced again.

The ID get's created and stored by iTunes when the song is synced on the device. If it's unsynced, the ID get's deleted.

Legman answered 2/9, 2012 at 15:37 Comment(0)
S
2

In case somebody else lands here, like me, using a Google search:

I confirmed what middaparka said above after an iOS upgrade of my device, when my music app tried to use persistentIDs from before the upgrade. The IDs had changed, and I ended up (unwittingly) listening to many songs from my library that I don't normally listen to...

So I took middaparka's advice and constructed a persistentKey by exclusive-oring the hashes from title, artistName, albumTitle and duration. Building the persistentKey during Core Database initialization will be save time later, by avoiding multiple string comparisons when fetching items in "normal operating code."

The persistentKey strategy worked properly for songs. However, when I made a hash for albums from title, artist and releaseYear, I ended up with one collision.

I had two self-titled albums by different artists released in 1976. When the hashes for the album title and the artist were exclusive-ored, they cancelled each other out. I ended up using the hash for the duration instead of the artist, and that worked.

I may end up refining the algorithm for generating the persistentKeys later...

Sergu answered 29/7, 2015 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.