mike vorisis mike vorisis - 28 days ago 7
iOS Question

Multidimensional Array in Realm Swift (Maximum depth exceeded error)

I want to make a 3D Array with Realm and I use List to accomplish this.
This is the code for my RealmDatabase:

class Sensors : Object {


dynamic var type = ""
dynamic var name = ""

let buttonAr = List<buttons>()

}

class buttons: Object{
let buttonsAdded = List<objectNameTopic>()
}

class objectNameTopic: Object {
dynamic var name = ""
dynamic var topic = ""
}


And this is what I use to call it:

var saving = Sensors()
var saving2 = objectNameTopic()
var but = buttons()
var array = ["on", "off"]
var array2 = ["1","2"]
var allSensors = useOfRealm.objects(Sensors.self)

override func viewDidLoad() {
super.viewDidLoad()


addSensor()

for i in 0...array.count-1 {
addSensor2(name: array[i], topic: array2[i])

}

if allSensors.count > 0 {
print(allSensors)
}

}


func addSensor() {

try! useOfRealm.write {

saving.type = "topic1"
saving.name = "mike"
useOfRealm.add(saving)


}

}

func addSensor2(name: String, topic: String) {

try! useOfRealm.write {

saving2.name = name
saving2.topic = topic
useOfRealm.add(saving2)

but.buttonsAdded.append(saving2)
saving.buttonAr.append(but)

}

}


This is what I get when I print the results:

Results<Sensors> (
[0] Sensors {
type = topic1;
name = mike;
buttonAr = RLMArray <0x6100000fd900> (
[0] buttons {
buttonsAdded = RLMArray <0x6100000fdb00> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
},
[1] buttons {
buttonsAdded = RLMArray <0x6180000fd180> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
}
);
}
)


Any Ideas of what I'm missing?

Thanks in advance

Answer

If the <Maximum depth exceeded> in the XCode console output is what you fear, fear no more. It's just a way the console is telling you that the object hierarchy is too deep for showing it to you, but the object(s) are there accessible through code.

Just try to print the content of your result by iterating through it and printing each child object instead of printing only the root.

Comments