Independent Independent - 4 months ago 35
Swift Question

Realm.objects(Type) return empty objects

I'm a new in Xcode development and I need some help with Realm database
.. these are my tables

class Parent :Object{
var id : Int=0
var name :String=""

}

class Surah :Parent{

dynamic var ayatNum = 0

private var fav : Bool {
set { self.fav = newValue }
get { return self.fav }
}


init(id: Int , ayat:Int , name:String) {
super.init()
super.id = id
self.ayatNum = ayat
super.name = name

}

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

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init()
}

required init(value: AnyObject, schema: RLMSchema) {
super.init()
}

required init() {
super.init()
}
}





class Reader : Parent{
private var fav : Bool {
set { self.fav = newValue }
get { return self.fav }
}


init(id: Int , name:String) {
super.init()
self.id = id
self.name = name

}
override static func primaryKey() -> String? {
return "id"
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init()
}

required init(value: AnyObject, schema: RLMSchema) {
super.init()
}

required init() {
super.init()
}

}


and when I call these lines and store the results in an array and print it

realm = try!Realm()
readers = Array(realm.objects(Reader.self))
print(readers)


and I try this one also

readers = Array(try!Realm().objects(Reader.self))
print(readers)


it prints an empty objects

Reader {
id = 0;
name = ;
}, Reader {
id = 0;
name = ;
}, Reader {
id = 0;
name = ;
}


I search about this problem in stackoverflow and find that solution which did not solve my problem

Realm.objects() returns empty objects

can any one help me !!

Answer

Because your Parent class properties aren't declared as dynamic. Most of the property in the Realm must be declared as dynamic (Exception is the List and LinkingObjects). Realm loaded all values lazily for performance. All property access is replaced specialized accessor at runtime. So you should declare the properties as dynamic. Like the following:

class Parent :Object{
    dynamic var id: Int=0
    dynamic var name: String=""
}

See https://realm.io/docs/swift/latest/#cheatsheet

Also, Surah class and Reader class will be much more simpler using convenience initializer. If so, You don't need to override init(realm: RLMRealm, schema: RLMObjectSchema), init(value: AnyObject, schema: RLMSchema) and init().

class Surah :Parent {
    dynamic var ayatNum = 0

    private var fav : Bool {
        set { self.fav = newValue }
        get { return self.fav }
    }

    convenience init(id: Int , ayat:Int , name:String) {
        self.init()
        self.id = id
        self.ayatNum = ayat
        self.name = name

    }
}

class Reader : Parent {
    private var fav : Bool {
        set { self.fav = newValue }
        get { return self.fav }
    }

    convenience init(id: Int , name:String) {
        self.init()
        self.id = id
        self.name = name
    }

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

}