Realm Object returning nil (Swift)
Asked Answered
C

2

5

I have a custom polygon object so I can save map overlays to Realm. I'm able to create this objects successfully, but when I want to retrieve the var polygon object it returns nil. When I print the polygon object, it prints it out fine, with all the data.

This is a sample of what it prints out.

CustomPolygon {
    name = Polygon1;
    id = p1;
    polygon = Polygon {
        coordinates = RLMArray <0x7f928ef36230> (
            [0] Coordinate {
                latitude = -36.914167;
                longitude = 174.904722;
            },
            [1] Coordinate {
                latitude = -36.9325;
                longitude = 174.957222;
            }, . . . 
        );
    };
}

My Function for loading data from Realm

func loadOverlaysFromRealm(){

    do {

        let realm = try Realm()

        let polygons = realm.objects(CustomPolygon)

        for p in polygons {

            var coordinates = [CLLocationCoordinate2D]()

            print(p) // !!!!! prints out what is above
            print(p.polygon) // !!!!! Returns nil. 

            if let coordinateList = p.polygon?.coordinates as? List<Coordinate> {

                for coordinate in coordinateList {
                    coordinates.append(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude))
                }
            }
            print(coordinates) // prints "[]"
            let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
            self.map.addOverlay(polygon)

        }

    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

My Classes

class CustomPolygon: Object {

    var name:String = ""
    var id:String = ""
    var polygon:Polygon? = nil

}

class Polygon: Object {
    var coordinates = List<Coordinate>()
}

class Coordinate: Object {
    var latitude:Double = 0.0
    var longitude:Double = 0.0
}
Clarita answered 30/5, 2016 at 5:3 Comment(0)
K
11

The String, Double and Object properties of your Object subclasses need to be declared with the dynamic modifier to allow Realm to override the getter and setter of the property. Without this the Swift compiler will access the object's instance variable directly, which doesn't provide any opportunity for Realm to read or write data from the Realm file. See Realm's model property cheatsheet for a quick overview of how to declare properties of each of the supported types.

Kowalewski answered 30/5, 2016 at 5:14 Comment(2)
problem exists even after using dynamic modifier, when i inspect realm file on realm studio , everything is fine, but on code it is returning value form instance variable directly.Arcboutant
I'd strongly suggest that you ask a new question, providing sufficient information for people to be able to help you, rather than commenting on a resolved, 3+ year old question.Kowalewski
C
2

If you are having issues with try Realm() returning nil, try removing the App from your simulator and rebuilding. Changing the Object properties as you develop can cause try Realm() to return nil.

Carapace answered 22/7, 2018 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.