How do i prevent duplicates in RealmSwift List?

∥☆過路亽.° 提交于 2019-12-22 05:24:21

问题


How do I prevent adding duplicates to a list in RealmSwift?

I have my User as a realm object, but the real data source is a server (simply caching the user locally with 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 that their in sync wrt list of tracks and etc.). I'm worried that if i loop over those lists from the server, appending to myUser.playlists, that I may end up adding the same playlist to the user's list of playlists multiple 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"
    }
}

回答1:


It depends on what kind of data coming from the server. If entire playlist data always come (you can always replace existing playlist data), you can just remove the list to empty, then append them.

realm.write {
    user.playlists.removeAll() // empty playlists before adding

    for playlistData in allPlaylistData {
        let playlist = Playlist()
        ...
        user.playlists.append(playlist)
    }
}

If differential data coming from the server (also some are duplicated), you have to check whether the data already exists.

realm.write {
    for playlistData in allPlaylistData {
        let playlist = Playlist()
        ...

        realm.add(playlist, update: true) // Must add to Realm before check

        guard let index = user.playlists.indexOf(playlist) else {
            // Nothing to do if exists
            continue
        }
        user.playlists.append(playlist)
    }
}



回答2:


I optimize the answer from @kishikava katsumi

All playlist objects are added to the database in one call instead of adding the in for loop. And also instead of returning index for every playlist I used faster function user.playlists.contains(playlist)

Here is the code. Thanks to the @kishikava katsumi for setting it up.

let fetchedPlaylists: [Playlist] = ...

try! realm.write {
  realm.add(fetchedPlaylists, update: true)
  for playlist in fetchedPlaylists {
    guard !user.playlists.contains(playlist) else {
      continue
    }
    user.playlists.append(playlist)
  }
}


来源:https://stackoverflow.com/questions/38513937/how-do-i-prevent-duplicates-in-realmswift-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!