How to prevent duplicates from being added to the list in RealmSwift?
I have Usera realm object, but the real data source is the server (just caching the user locally using Realm). When I get the current user data from my server, I want to make sure that my user stored in realm has all the playlists coming from the server (and their synchronization according to the list of tracks, etc.). I worry that if I go through these lists from the server by adding to myUser.playlists, I can end up adding the same playlist to the list of playlists several times.
class User: Object {
dynamic var name = ""
dynamic var id = ""
let playlists = List<Playlist>()
override class func primaryKey() -> String {
return "id"
}
}
class Playlist: Object {
dynamic var name = ""
dynamic var id = ""
let tracks = List<Song>()
override class func primaryKey() -> String {
return "id"
}
}
class Song: Object {
dynamic var title = ""
let artists = List<Artist>()
dynamic var id = ""
override class func primaryKey() -> String {
return "id"
}
}
class Artist: Object {
dynamic var name = ""
dynamic var id = ""
override class func primaryKey() -> String {
return "id"
}
}