Evgeniy Kleban
Evgeniy Kleban

Reputation: 6965

Swift realm return zero objects

I have opened my realm database in Realm browser, i can see that there is are actual data (10 entities).

But when i call print("realm objects \(self.realm.objects(CharacterModel.self))")

Result is empty:

realm objects Results<CharacterModel> <0x7f8d8f204a30> (

)

When i put breakpoint and check data base state at this moment data exist. Why is that happening?

realm is declared like that:

 static func realm() -> Realm{
        do {
            let realm = try Realm()
            return realm
        } catch let error as NSError {

            fatalError("Error opening realm: \(error)")
        }
    }

Upvotes: 1

Views: 222

Answers (1)

Jay
Jay

Reputation: 35667

The answer may reveal itself if we eliminate some variables:

The following code works with a Realm that contains Person() objects

func doPrintData() {
    do {
        let realm = try Realm()
        print("realm objects \(realm.objects(Person.self))")
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

the following also works

func realm() -> Realm{
    do {
        let realm = try Realm()
        return realm
    } catch let error as NSError {

        fatalError("Error opening realm: \(error)")
    }
}

func doPrintData() {
    do {
        let realm = self.realm()
        print("realm objects \(realm.objects(Person.self))")
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

There's probably more code involved but try one of the above solutions and see if it makes a difference.

Upvotes: 1

Related Questions