Luis
Luis

Reputation: 533

Realm <List> populated but just with one object

First time i use Realm, i use it in a Swift Project. I use the last version of Realm 0.96.3.

I got two Realm Models: Jog and Marker

class RealmCurrentRecJog: Object {

        dynamic var id: Int = 0

        dynamic var duration: Double = 0.0
        let markers = List<RealmCurrentRecMarker>()

        // Specify properties to ignore (Realm won't persist these)

        override static func primaryKey() -> String? {
            return "id"
        }
    }

class RealmCurrentRecMarker: Object {

    dynamic var order: Int = 0

    dynamic var latitude: Double = 0.0
    dynamic var longitude: Double = 0.0

    dynamic var speed: Double = 0.0
    dynamic var accuracy: Double = 0.0
    dynamic var altitude:  Double = 0.0

    dynamic var time: Double = 0.0
}

As you can see in the Jog model i got a <List> of Markers and id is the primary key. In the Marker model nothing crazy.

So i fill my Realm Models with this function:

private func fillDbWithMarker() {

        let saveJog = RealmCurrentRecJog()
        let saveMarker = RealmCurrentRecMarker()

        let jogToSave = self.jogRecorder.getDraftJog()

        saveJog.id = Int(jogToSave.id)
        saveJog.duration = jogToSave.duration

        saveMarker.order = Int((jogToSave.markers.last?.order)!)
        saveMarker.longitude = (jogToSave.markers.last?.longitude)!
        saveMarker.latitude = (jogToSave.markers.last?.latitude)!

        saveMarker.accuracy = (jogToSave.markers.last?.accuracy)!
        saveMarker.altitude = (jogToSave.markers.last?.altitude)!
        saveMarker.speed = (jogToSave.markers.last?.speed)!
        saveMarker.time = (jogToSave.markers.last?.time)!

        do {
            let realm = try Realm()
            do {
                try! realm.write {
                    saveJog.markers.append(saveMarker)
                    realm.add(saveJog, update: true)
                }
            }
        } catch let error as NSError {
            print(error)
        }
    }

And at the end when i look at my Realm Browser the result is that i just got the last marker in my list even if my Marker table is full. I don't understand where is my mistake .

RealmBrowser

Edit: Here is the code to make it work

do {
            let realm = try Realm()
            do {
                try! realm.write {
                    saveJog = realm.objects(RealmCurrentRecJog).last!
                    saveJog.markers.append(saveMarker)
                    realm.add(saveJog, update: true)
                }
            }
        } catch let error as NSError {
            print(error)
        }

Upvotes: 1

Views: 347

Answers (1)

bdash
bdash

Reputation: 18308

fillDbWithMarker creates a Jog whose markers list contains a single marker. When it's saved with Realm.add(_:update:), it updates the existing Jog with the same primary key so that its properties match the passed-in object's properties. This results in the existing Jog object being updated to contain only the single new marker.

If you tweak fillDbWithMarker to retrieve the existing Jog with the given ID and append the marker to its markers list you'll get the behavior you're after.

Upvotes: 3

Related Questions