Skip to content

Instantly share code, notes, and snippets.

@Jerrot
Last active March 17, 2024 13:10
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save Jerrot/fe233a94c5427a4ec29b to your computer and use it in GitHub Desktop.
Save Jerrot/fe233a94c5427a4ec29b to your computer and use it in GitHub Desktop.
Transform arrays with ObjectMapper to Realm's List type
// Based on Swift 1.2, ObjectMapper 0.15, RealmSwift 0.94.1
// Author: Timo Wälisch <timo@waelisch.de>
import UIKit
import RealmSwift
import ObjectMapper
import SwiftyJSON
class ArrayTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
typealias Object = List<T>
typealias JSON = Array<AnyObject>
let mapper = Mapper<T>()
func transformFromJSON(value: AnyObject?) -> List<T>? {
var result = List<T>()
if let tempArr = value as! Array<AnyObject>? {
for entry in tempArr {
let mapper = Mapper<T>()
let model : T = mapper.map(entry)!
result.append(model)
}
}
return result
}
// transformToJson was replaced with a solution by @zendobk from https://gist.github.com/zendobk/80b16eb74524a1674871
// to avoid confusing future visitors of this gist. Thanks to @marksbren for pointing this out (see comments of this gist)
func transformToJSON(value: Object?) -> JSON? {
var results = [AnyObject]()
if let value = value {
for obj in value {
let json = mapper.toJSON(obj)
results.append(json)
}
}
return results
}
}
// SampleModel.swift
// Author: Timo Wälisch <timo@waelisch.de>
import UIKit
import ObjectMapper
import RealmSwift
import SwiftyJSON
class SampleModel: Object, Mappable {
// MARK: Realm - stored properties
dynamic var title: String = ""
var products = List<ProductModel>()
// MARK: ObjectMapper
class func newInstance(map: Map) -> Mappable? {
return SampleModel()
}
/// Mapping between ObjectMapper (JSON) and the model properties
func mapping(map: Map) {
title <- map["title"]
products <- (map["products"], ArrayTransform<ProductModel>())
}
}
@marksbren
Copy link

This is giving me some issues when calling the transformToJSON part. It doesn't seem to be correctly transforming my list of RealmPosts to JSON correctly. Have you experienced any of these issues? Here is the relevant code:

My RealmPheLevel Model

import Foundation
import RealmSwift
import ObjectMapper

class RealmLevel: Object, Mappable {
    dynamic var id: String?
    dynamic var safeId: String?
    dynamic var createdAt: Double = 0.00
    dynamic var updatedAt: Double = 0.00
    dynamic var timeTaken: Double = 0.00
    dynamic var user: RealmUser?
    var posts = List<RealmPost>()
    dynamic var value: Double = 0.00
    dynamic var notes: String?

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

    required convenience init?(_ map: Map){
        self.init()
    }

    // Mappable
    func mapping(map: Map) {

        if id == nil {
            id <- map["objectId"]
        }
        safeId      <- map["objectId"]
        createdAt   <- (map["createdAt"], RealmDateTransform())
        updatedAt   <- (map["updatedAt"], RealmDateTransform())
        timeTaken   <- (map["timeTaken"], RealmDateTransform())
        user        <- map["user"]
        posts       <- (map["posts"], ArrayTransform<RealmPost>())
        value       <- map["value"]
        notes       <- map["notes"]

    }



}

When calling the mapper it returns nil

let JSONString = Mapper().toJSONString(realmLevel)

When I print out the JSON strings for the individual components the "posts" array is incorrectly formatted. It looks like it is not parsing the RealmPost from the List of RealmPosts I am sending it. Instead, it is just formatting it as a giant string. Have any of you experienced this?

["timeTaken": 2015-11-18T17:53:10.903Z, "objectId": iBjaiZEYng, "user": {
    birthday = "1/1/111";
    createdAt = "2015-03-17T23:08:16.000Z";
    email = "xxx@xxx.com";
    "first_name" = Mark;
    gender = male;
    id = xxx;
    "last_name" = xxx;
    name = "Mark xxx";
    objectId = xxx;
    updatedAt = "2015-11-16T12:27:51.000Z";
}, "createdAt": 2015-11-18T17:53:11.501Z, "value": 9, "posts": (
    "RealmPost {\n\tphotoUrl = http:\\image.png;\n\tfavorites = 0;\n\tdesc = xxxx;\n\tstatus = published;\n\tid = xxxxx;\n\tsafeId = xxxx;\n\tcreatedAt = 185534253.873;\n\tupdatedAt = 185534268.349;\n\tuser = RealmUser {\n\t\tid = xxx;\n\t\tsafeId = xxx;\n\t\tcreatedAt = 164326096;\n\t\tupdatedAt = 185372871;\n\t\temail = xxx@xxx.com;\n\t\tgender = male;\n\t\tname = Mark xxx;\n\t\tfirstName = Mark;\n\t\tlastName = xxx;\n\t\tfbId = xxx;\n\t\tbirthday = 1111-11-11 00:00:00 +0000;\n\t\tprofileUrl = (null);\n\t\tlocation = (null);\n\t};\n}",
    "RealmPost {\n\tphotoUrl = http:\\image.png;\n\tfavorites = 0;\n\tdesc = xxxx;\n\tstatus = published;\n\tid = xxxxx;\n\tsafeId = xxxx;\n\tcreatedAt = 185534253.873;\n\tupdatedAt = 185534268.349;\n\tuser = RealmUser {\n\t\tid = xxx;\n\t\tsafeId = xxx;\n\t\tcreatedAt = 164326096;\n\t\tupdatedAt = 185372871;\n\t\temail = xxx@xxx.com;\n\t\tgender = male;\n\t\tname = Mark xxx;\n\t\tfirstName = Mark;\n\t\tlastName = xxx;\n\t\tfbId = xxx;\n\t\tbirthday = 1111-11-11 00:00:00 +0000;\n\t\tprofileUrl = (null);\n\t\tlocation = (null);\n\t};\n}"), 
"updatedAt": 2015-11-18T17:53:11.501Z]

And here is my ArrayTransform file:

import UIKit
import RealmSwift
import ObjectMapper
import SwiftyJSON

class ArrayTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
    typealias Object = List<T>
    typealias JSON = Array<AnyObject>

    func transformFromJSON(value: AnyObject?) -> List<T>? {
        let result = List<T>()
        if let tempArr = value as! Array<AnyObject>? {
            for entry in tempArr {
                let mapper = Mapper<T>()
                let model : T = mapper.map(entry)!
                result.append(model)
            }
        }
        return result
    }

    func transformToJSON(value: List<T>?) -> Array<AnyObject>? {
        if (value?.count > 0)
        {
            var result = Array<T>()
            for entry in value! {
                result.append(entry)
            }
            return result
        }
        return nil
    }
}

@marksbren
Copy link

FYI, I believe I fixed the issue. I changed the transformToJSON function to the one in this example:
https://gist.github.com/zendobk/80b16eb74524a1674871

@Jerrot
Copy link
Author

Jerrot commented Nov 25, 2015

Thank you very much @marksbren. And sorry for the late answer, GitHub still doesn't send me notifications for comments on gists :(
You are right, I never needed the toJSON part and your experience is pretty obvious actually. I'll update the gist with a reference to your example to avoid confusing other visitors.

@theladyjaye
Copy link

Line 31 in this rev of the gist:
https://gist.github.com/Jerrot/fe233a94c5427a4ec29b/880ba0a531e522fe5fba5884485bed067f629d51

(where it's referencing the modification from @zendobk) is missing the instance var located on line 9 from the source:
https://gist.github.com/zendobk/80b16eb74524a1674871#file-listtransform-swift-L9

@sudeep23
Copy link

Can Anyone Please help me to Map and Realm with following Classes. Main problem is I need List of Int but Realm doesn't support List of Int type so kind of stuck....

    class OfficeInformationData: Object, Mappable{
        dynamic var id: String?
        dynamic var officeName: String?
        dynamic var titlePost: String?
        dynamic var location: LocationData?
        dynamic var jobType = 0
        var officeTiming: OfficeTimingData?
        dynamic var timestamp: Int = 0
        dynamic var officeiLoop:Bool = false


        required convenience init?(_ map: Map) {
            self.init()
        }

        func mapping(map: Map) {
            id <- map["id"]
            officeName <- map["officeName"]
            titlePost <- map["titlePost"]
            location <- map["location"]
            jobType <- map["jobType"]
            officeTiming <- map["officeTimings"]
            timestamp <- map["timestamp"]
            officeiLoop <- map["officeiLoop"]


        }
    }

     class OfficeTimingData: Mappable{
        var fromHour:String?
        var toHour:String?
        var days: [Int]?

         required init?(_ map: Map) {

        }

        func mapping(map: Map) {
            fromHour <- map["fromHour"]
            toHour <- map["toHour"]
            days <- map["days"]
        }
    }

@bent0b0x
Copy link

bent0b0x commented Jan 7, 2016

I am running into an issue with the list transform code that @marksbren posted.

In my particular example, I have two models: User and Game, which have a many to many relationship. Say I have a User object that has already been saved to Realm, and I append it to a Game's list of Users, then the next time I try to map that Game to JSON I hit the following exception:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Primary key can't be changed after an object is inserted.'

The exception is thrown when I hit the "id" line in my User model's mapping method:

func mapping(map: Map) {
    firstName           <- map["first_name"]
    lastName            <- map["last_name"]
    email               <- map["email"]
    authenticationToken <- map["authentication_token"]
    id                  <- map["id"]
    games               <- (map["games"], ListTransform<Game>())
}

This sort of makes sense to me; since my User model has already been saved to the DB, and id is my model's primary key, the fact that the mapping method is attempting to modify the id property in any way (even if it is the same value) is a no-no. However, I cannot find any way to get around this. Are these transforms only supposed to work with objects that have yet to be persisted/do not have primary keys? Could be my inexperience, but any help is appreciated!

@timefrancesco
Copy link

@Jerrot you forgot

let mapper = Mapper()

when taking the code from @zendobk 's gist

@grimabe
Copy link

grimabe commented Feb 7, 2016

Having same problem than @bent0b0x

@bent0b0x did you find a solution ?

@HocTran
Copy link

HocTran commented Mar 25, 2016

@bent0b0x, @grimabe

try! realm.write {
       realm.add(<your object>, update: true)
}

@Jerrot
Copy link
Author

Jerrot commented Apr 12, 2016

Sorry for the late responses again, I just found out about third-party services to get notifications for gist comments.

@xeo-it: Indeed, thank you. Added the line now.
@bent0b0x: I guess the hint to the update parameter by @HocTran already helped you out.

Since this gist was based on Swift 1.2 and older versions of Realm and ObjectMapper, I can't really apply more (tested) changes to it anymore. My own code for this looks very different today and I obviously didn't know how to properly use map() only 8 months ago. 😉 I'll consider creating a new gist for current Swift/Realm/ObjectMapper versions.

@winkelsdorf
Copy link

+1 for the new gist ;)

@bourgeois
Copy link

Thanks for this, it works great !

However, when I try to serialize data back to JSON from realm (to upload request to web service from database), Realm throws the exception Object has been deleted or invalidated. The farthest I could go in debugging is when it goes inside the map() function, and it crashes as soon as it tries to execute property <- map['attribute']

Anyone experienced that with the Realm/ObjectMapper combo ? It only happens when my objects are persisted. If it's not persisted, I can generate my JSON without any issues.

@rkittinger
Copy link

rkittinger commented May 20, 2016

How would I get this to work with Alamofire? Not using SwiftyJSON.
With this line: ListTransform< Aliases > ()
I get Type Aliases does not conform to protocol Mappable.

Aliases is of type 'Object' from RealmSwift.

I want to turn a [String]-Array into a List object.... or convert the array to a json string to be saved in Realm, so I can convert back to Array later.

@Ryan0520
Copy link

Ryan0520 commented Jul 1, 2016

@marksbren thanks your advice, you solve my problem that can't get the List model. This is work ,when I do this

    func mapping(map: Map) {
        departmentName <- map["department_name"]
        employees <- (map["employees"], ListTransform<Employee>())
        id <- map["id"]

    }

the employees is not null, thank you very much!

@tristanhimmelman
Copy link

It would be great is the ArrayTransform could be added to ObjectMapper. Please send over a PR

@Taco55
Copy link

Taco55 commented Sep 4, 2016

I have an issue with ArrayTransform to serialize nested objects for use with Alamofire. The serialization end up with a SwiftDeferred array (see output in the example code). What would be the proper way to use ArrayTransform?

class Example: Object, Mappable {
    dynamic var foo: String = "bar"
    var qux = List<Qux>()

    required convenience init?(_ map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        self.foo <- map["foo"]
        self.qux <-  (map["qux"], ArrayTransform<Qux>())
    }
}

class Qux: Object, Mappable {
    dynamic var x = 1

    required convenience init?(_ map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        self.x <- map["x"]
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let realm = try! Realm()
        if realm.isEmpty {
            try! realm.write {
                let example = Example()
                example.qux.appendContentsOf([1,2,3].map { Qux(value: [$0]) })
                realm.add(example)
            }
        }

        let example = realm.objects(Example.self).first!

        try! realm.write {
            print( Mapper().toJSON(example) )

                /*
                // Gives a SwiftDeferredNSArray
                ["qux": <_TtCs21_SwiftDeferredNSArray 0x7fa9904cdc30>(
                    {
                        x = 1;
                    },
                    {
                        x = 2;
                    },
                    {
                        x = 3;
                    }
                    )
                    , "foo": bar]
                 */

            print( Mapper().toJSONString(example, prettyPrint: true)! )
            /* 
            // Gives output as expected
                {
                    "qux" : [
                    {
                        "x" : 1
                    },
                    {
                        "x" : 2
                    },
                    {
                        "x" : 3
                    }
                    ],
                    "foo" : "bar"
                }
             */

        }


        // Expected result would be:
        let fieldsExampleObject = [ "foo": "bar", "qux": [ ["x": 1], ["x": 2], ["x": 3] ] ]
        print(fieldsExampleObject)
        /*
             ["qux": <__NSArrayI 0x7fdd936c00c0>(
                 {
                    x = 1;
                 },
                 {
                    x = 2;
                 },
                 {
                    x = 3;
                 }
             ),
             "foo": bar]
        */

    }

}

@fahlout
Copy link

fahlout commented Nov 2, 2016

Any way to get a version that works with swift 3 and swift 3 Realm?

@s-petersson
Copy link

Here is a version that should work with Swift 3 :) @fahlout

import UIKit
import RealmSwift
import ObjectMapper

class ArrayTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
    typealias Object = List<T>
    typealias JSON = Array<AnyObject>
    
    func transformFromJSON(_ value: Any?) -> List<T>? {
        let realmList = List<T>()

        if let jsonArray = value as? Array<Any> {
            for item in jsonArray {
                if let realmModel = Mapper<T>().map(JSONObject: item) {
                    realmList.append(realmModel)
                }
            }
        }

        return realmList
    }
    
    func transformToJSON(_ value: List<T>?) -> Array<AnyObject>? {

        guard let realmList = value, realmList.count > 0 else { return nil }

        var resultArray = Array<T>()

        for entry in realmList {
            resultArray.append(entry)
        }

        return resultArray
    }
}

@RajaveluC
Copy link

RajaveluC commented Dec 21, 2016

@Pendla
I am using the above swift 3.0 version of ArrayTransform and I still couldn't get around parsing nested objects succesfully.

Here are my classes.

class User : Object, Mappable {

    dynamic var userId: String!
    dynamic var emailId: String!
    
    override class func primaryKey() -> String? {
        return "userId"
    }
    
    required convenience init?(map: Map) {
        self.init()
    }
    
    func mapping(map: Map) {
        userId <- map["userId"]
        emailId <- map["email"]
    }
}

class ContactGroup : Object, Mappable {
    dynamic var groupId: String!
    var users = List<User> ()
    
    override class func primaryKey() -> String? {
        return "groupId"
    }
    
    required convenience init?(map: Map) {
        self.init()
    }
    
    func mapping(map: Map) {
        groupId <- map["contactGroupId"]
        users <- (map["users"], ArrayTransform<User>())
    }
}

func storeContactGroups (groups : NSArray)  {
        var groupRealms = Array<ContactGroup> ()
        for object in groups {
            let group : ContactGroup! = ContactGroup(JSON: object as! [String : Any])
            groupRealms.append(group)
        }
        
        let realm = try! Realm()
        try! realm.write {
            for group in groupRealms {
                realm.add(group, update: true)
            }
        }
 }

After the storeContactGroups is executed, I still see zero Users in my Realm file under ContactGroup. What am I doing wrong here?

screen shot 2016-12-21 at 2 52 23 pm

@danipralea
Copy link

danipralea commented Dec 23, 2016

I'm having the same error. Mapping succeeds, but somehow saving the object (with the list of objects) invalidates the list and sets that list to nil.
And from what I've seen, you don't even need the extension for ArrayTransform. The latest version of ObjectMapper successfully maps an array of objects.

@borut-t
Copy link

borut-t commented Jan 11, 2017

Here is the simplified version using swift 3+

func transformToJSON(_ value: Object?) -> JSON? {
  var results = [[String:Any]]()
  if let value = value {
    results.append(contentsOf: value.map({ self.mapper.toJSON($0) }))
  }
  return results as ArrayTransform.JSON?
}

@pabloruan0710
Copy link

Very Crazy, if properties is managed in Realm, the correct is used let for declaration of List and not var ?

@gulzatique
Copy link

gulzatique commented Apr 6, 2017

Thanks it worked for me But I needed to change it according to Swift warnings and errors and this is what I ended up with:

class ArrayTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
    typealias Object = List<T>
    typealias JSON = Array<AnyObject>
    
    let mapper = Mapper<T>()
    
    func transformFromJSON(_ value: Any?) -> List<T>? {
        let result = List<T>()
        if let tempArr = value as! Array<AnyObject>? {
            for entry in tempArr {
                let mapper = Mapper<T>()
                let model : T = mapper.map(JSON: entry as! [String : Any])!
                result.append(model)
            }
        }
        return result
    }
    
    func transformToJSON(_ value: Object?) -> JSON? {
        var results = [AnyObject]()
        if let value = value {
            for obj in value {
                let json = mapper.toJSON(obj)
                results.append(json as AnyObject)
            }
        }
        return results
    }
}

@calvinsug
Copy link

What if i want to Map my List of Integer or String object? @pendla
I still can't solve it

class X: Object, StaticMappable {
 var strings = List<RealmString>()

 class func objectForMapping(map: Map) -> BaseMappable? {
     return X()
 }

func mapping(map: Map) {
      strings <- (map["strings"], ListTransform<RealmString>())
}
}

class RealmString: Object, StaticMappable {
 dynamic var value = ""
 
 class func objectForMapping(map: Map) -> BaseMappable? {
     return RealmString()
 }
 
 func mapping(map: Map) {
     value <- map
 }

}

Json should be like this :
{
x: ["a","b","c"]
}

@RabbitMC
Copy link

RabbitMC commented Dec 8, 2017

Thanks for sharing this code!

Here is my solution for Swift 4.0.2 & Xcode 9.2

import RealmSwift
import ObjectMapper

class ArrayTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
    typealias Object = List<T>
    typealias JSON = Array<AnyObject>

    func transformFromJSON(_ value: Any?) -> List<T>? {
        let result = List<T>()
        if let tempArr = value as! Array<AnyObject>? {
            for entry in tempArr {
                let mapper = Mapper<T>()
                let model : T = mapper.map(JSONObject: entry)!
                result.append(model)
            }
        }
        return result
    }

    func transformToJSON(_ value: List<T>?) -> Array<AnyObject>? {
        if (value!.count > 0) {
            var result = Array<T>()
            for entry in value! {
                result.append(entry)
            }
            return result
        }
        return nil
    }
}

@nadia-am
Copy link

nadia-am commented Jan 1, 2018

@sudeep23 did you find any solution? i have the same problem.

@levibostian
Copy link

@calvinsug I have encountered that problem myself. Storing a list of primitive data types in a Realm List apposed to storing a Realm List of Realm Models.

I give a possible solution here: https://stackoverflow.com/a/54581186/1486374

@ElonPark
Copy link

Thanks

I 'm use not optional forced unwrapping
This is my solution for Swift 4.2 & Xcode 10.1

import RealmSwift
import ObjectMapper

class ArrayTransform<T: RealmSwift.Object>: TransformType where T: Mappable {
    typealias Object = List<T>
    typealias JSON = Array<AnyObject>
    
    /**
     - Parameter value: JSON Value
     - Returns: if value is `nil` or not Array will be return empty List<T>
    */
    func transformFromJSON(_ value: Any?) -> Object? {
        let result = Object()
        guard let _value = value,
            let objectArray = _value as? Array<AnyObject> else { return result }

        let mapper = Mapper<T>()
        
        for object in objectArray {
            //if model is `nil` continue to next object
            guard let model = mapper.map(JSONObject: object) else {
                continue
            }
            
            result.append(model)
        }
        
        return result
    }
    
    /**
     - Parameter value: RealmSwift Object
     - Returns: if value is `nil` or empty will be return empty Array<AnyObject>
     */
    func transformToJSON(_ value: Object?) -> JSON? {
        var result = JSON()
        guard let _value = value, _value.count > 0 else { return  result }
        
        result = _value.map { $0 }
      
        
        return result
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment