3

My class has all properties as dynamic but still when retrieving them from realm i get a collection of empty objects, and check the realm db with the realm browser and the data is there, this is my class:

class ProjectEntity: Object {

    /**
    Property: All properties of the ProjectEntity 
    **/
    dynamic var ProjectId = 0
    dynamic var ProjectTitle = ""
    dynamic var ProjectSubtitle = ""
    dynamic var ProjectType = ""
    dynamic var ProjectClass = ""
    dynamic var ProjectCoordinates = ""
    dynamic var ProjectGraphType = ""
    dynamic var ProjectModifiedOn = NSDate(timeIntervalSince1970: 0)
    dynamic var ProjectCity = ""
    dynamic var ProjectCounty = ""
    dynamic var ProjectZip = ""

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

    func getShape() -> MapShape{
        let adapter = ProjectsJSONAdapter()
        let shape: MapShape = adapter.parseShape(id: self.ProjectId, type: self.ProjectGraphType, jsonStr: self.ProjectCoordinates)
        return shape
    }
}

here is how i'm reading the data:

    let projectsList = realm.objects(ProjectEntity)
    for project in projectsList {
        projects.append(project)//The properties in project have all their default/empty values
    }

any ideas?

2 Answers2

1

How are you retrieving the persisted ProjectEntity objects? The following code snippet should do the trick:

let entities = Realm().objects(ProjectEntity)
segiddins
  • 4,110
  • 1
  • 17
  • 22
  • Thanks that is exactly what i'm doing but once i loop through the entity collection each ProjectEntity is empty: here it the code: let projectsList = realm.objects(ProjectEntity) for project in projectsList { projects.append(project) //this project has all its properties with their default/empty values } – Daniel Coellar Aug 26 '15 at 17:27
  • i read in other posts that adding "dynamic" to the props should fixt it, but it did not in my case, anything else i could be missing? – Daniel Coellar Aug 26 '15 at 17:31
  • Could you be more specific on what's unexpected about the behavior you're seeing? – segiddins Aug 26 '15 at 20:42
  • When i loop through the "for project in projectsList" it loops through it 582 times, but each project has all its properties empty. I opened the realm file with the realm browser and i see all 582 records with data; let me know if this clarifies it, and thanks again – Daniel Coellar Aug 26 '15 at 22:57
  • Are you viewing the objects in the debugger? If so, that's expected. Does the object still appear "empty" when calling `println`? – segiddins Aug 27 '15 at 02:21
  • 1
    exactly, the issue was i was only looking at the debuger, which always show them empty but if i do an println is show the data, so there was no issue really; thanks a lot for the help – Daniel Coellar Aug 27 '15 at 23:01
  • I saw the same problem too, in the debugger, the realm object is empty, led to a lot of misunderstanding, hope the realm team can fix it. – dickyj Mar 14 '16 at 08:38
0

first your class need to inherit RLMObject :

class ProjectEntity: RLMObject {
    ...
}

and after if you want all the ProjectEntity objects try this :

let allProjectEntityObjects: RLMResults = ProjectEntity.allObjects()

if you need some extra help you can follow this tutorial from Realm :

Building a To-Do App with Realm

Philippe
  • 1,567
  • 16
  • 18
  • Thanks but did not work, i get an error "ProjectEntity.Type does not have a member named 'allObjects'", i'm using realm 0.95.0 for swift if that helps – Daniel Coellar Aug 26 '15 at 16:04