Is it possible to store multi dimensional data array in realm?


I have a matrix (array of arrays of type int) I wish to store in realm.
Is it possibel ? if so how do I define the data type ?

example :



I know this is a Realm React question but let me pose a couple of options using some Swift code.

You ‘can’t’ store nested arrays directly in Realm as presented - only primatives. However, realm does support a more generic Data (NSData in objc) type. So you can encode your matrix into a Data type and store that.

Something along these lines should get you going:

let data = try NSKeyedArchiver.archivedData(withRootObject: myArray, requiringSecureCoding: false)

and then when reading it back

if let thisArray = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)

Another option, is to simply create an object to hold your datasets and then store those within another object List property. Here’s an example: suppose we want to store a list of location coordinates within a State. Here’s a location object to be persisted in Realm

class LocationClass: Object {
    @objc dynamic var latitude = ""
    @objc dynamic var longitude = ""
    convenience init(lat: String, lon: String) {
        self.latitude = lat
        self.longitude = lon

and then a State object to hold the locations within that state

class StateClass: Object {
    @objc dynamic var which_state = ""
    let locations = List<LocationClass>()
    //an alternative to get an actual matrix as described in the question 
    func getMultiArray() -> [ [String] ] {
        var outerArray = [ [String] ]()
        for loc in locations {
            var array = [String]()
        return outerArray

and then finally the code to create the objects and store them within Realm

        let loc0 = LocationClass(lat: "787", lon: "1280")
        let loc1 = LocationClass(lat: "866", lon: "1411")
        let loc2 = LocationClass(lat: "951", lon: "1493")
        let state = StateClass()
        state.which_state = "Florida"
        if let realm = gGetRealm() {
            try! realm.write {

Then, to test, we can either iterate over the stored locations OR if you want it back an an array as presented in the question, call the getMultiArray function

    if let realm = gGetRealm() {
        let results = realm.objects(StateClass.self)
        guard let aState = results.first else {return}
        for location in aState.locations {
            let lat = location.latitude
            let long = location.longitude
            print(lat, long)
        let test = aState.getMultiArray()

and the output

787 1280
866 1411
951 1493
[["787", "1280"], ["866", "1411"], ["951", "1493"]]


Thank you Jay for the detailed answer !

Happy 2019