Search code examples
iosswiftxcoderealmurlsession

How to prevent Realm from overwriting itself while parsing JSON to a Realm database in Swift?


I've got a problem with Realm. I want to write an external JSON script to my Realm database with Swift. But when doing that it overwrites the record in my database every loop. When I print the class print(songClass) it shows it correctly but when doing this

class SongR: Object {

    @objc dynamic var id: String?
    @objc dynamic var rawName: String?
    @objc dynamic var title: String?
    @objc dynamic var artist: String?
    @objc dynamic var album: String?
    @objc dynamic var playlist: String?
    @objc dynamic var link: String?

}


let realm = try! Realm()

let songClass = SongR()


if let url = URL(string: "http://somewebsite.com/json.php") {
            URLSession.shared.dataTask(with: url) { data, response, error in
                if let data = data {
                    let jsonDecoder = JSONDecoder()
                    do {
                        let parsedJSON = try jsonDecoder.decode([Song].self, from: data)
                        let main = DispatchQueue.main.async {
                            try! realm.write {
                                for song in parsedJSON {
                                    songClass.id = song.id
                                    songClass.rawName = song.rawName
                                    songClass.title = song.title
                                    songClass.artist = song.artist
                                    songClass.album = song.album
                                    songClass.playlist = song.playlist
                                    songClass.link = song.link
                                    realm.add(songClass)
                                    print(songClass)
                                }
                            }
                        }
                    } catch {
                        print(error)
                    }
                }
            }.resume()
        }

it overwrites itself and I only get the last value from the JSON file displayed in my Realm database.

Anyone got a clue how to fix this?

Also, I want to overwrite the whole Realm database with all the items from the JSON file every time I parse/update the JSON file and not add them to it. How do I do that?

Thanks in advance!


Solution

  • Found the solution. let songClass = SongR() needed to be inside the loop:

    let main = DispatchQueue.main.async {
                                try! realm.write {
                                    for song in parsedJSON {
                                        let songClass = SongR()
                                        songClass.id = song.id
                                        songClass.rawName = song.rawName
                                        songClass.title = song.title
                                        songClass.artist = song.artist
                                        songClass.album = song.album
                                        songClass.playlist = song.playlist
                                        songClass.link = song.link
                                        realm.add(songClass)
                                        print(songClass)
                                    }
                                }
                            }