3

If I use ObjectMapper with normal swift class, I am able to get JSON object created but when I use it with Realm class model, program crash. I tried to play around it ( check whether object exist, then using overriding primaryKey method but didn't help). I used class ListTransform from StackOverFlow and it seems to working fine. Xcode doesn't give any particular information on abend so that I can debug more. Other stackoverflow post doesn't help.

            class UserResponse: Object, Mappable {

                // MARK: Properties
                //
                var item = List<Item>()
                dynamic var itemPurchaseDate = NSDate()

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

                func mapping(map: Map) {
                    item    <- (map["item"], ListTransform<Item>())
                    itemPurchaseDate <- (map["itemPurchaseDate"], ISO8601DateTransform())
                }
            }

            class ListTransform<T:RealmSwift.Object where T:Mappable> : TransformType {
                typealias Object = List<T>
                typealias JSON = [AnyObject]

                let mapper = Mapper<T>()

                func transformFromJSON(value: AnyObject?) -> Object? {
                    let results = List<T>()
                    if let value = value as? [AnyObject] {
                        for json in value {
                            if let obj = mapper.map(json) {
                                results.append(obj)
                            }
                        }
                    }
                    return results
                }

                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
                }
            }

            class Item: Object, Mappable {

                // MARK: Properties
                //
                dynamic var itemName = ""

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

                // Mapping between ObjectMapper (JSON) and the model properties
                //
                func mapping(map: Map) {
                    itemName <- map["itemName"]
                }
            }

        class RealmManager {

            // returns a dictionary which represents give mappable object
            //
            func jsonFormat<N: Mappable>(object: N) -> [String: AnyObject] {
                return Mapper().toJSON(object)
            }

           func uploadDataToBackend(someObject: UserResponse) {
                let postData = jsonFormat(someObject)
                print(postData)
        } 
   }

    -----------------------------------------

    Program crash on following lines:
    1. In Item class, program crash on line         itemName <- map["itemName"]
    2. If I comment above line , then I get crash on itemPurchaseDate <- (map["itemPurchaseDate "], ISO8601DateTransform())

Also note that, when Item does't have anything, first lines execute but when it does have data, it just crash. If you need any more information, please let me know

4

1 回答 1

3

使用 ObjectMapper 将 Realm 模型序列化为 JSON 时,您需要在写入事务中进行。或者使用init(value:). 因为即使在序列化时,ObjectMapper 也会为模型的属性分配值。它只是分配相同的值,没有修改,但 Realm 不允许在没有事务的情况下分配任何值。

注意:使用 ObjectMappers 的toJSON函数生成领域对象的 JSON 字符串仅适用于领域写入事务。这是因为 ObjectMapperinout在其映射函数 ( <-) 中使用了标志,这些函数用于序列化和反序列化。Realm 检测到标志并强制toJSON在写入块内调用函数,即使对象没有被修改。

另请参阅 ObjectMapper 文档中的 ObjectMapper + Realm 部分。

https://github.com/Hearst-DD/ObjectMapper#objectmapper--realm

于 2016-06-23T17:13:48.220 回答