Giter Site home page Giter Site logo

Comments (20)

thacilima avatar thacilima commented on July 26, 2024 10

I also had this problem just when writing the id (realm object primary key) to json.

My solution bellow:

if map.mappingType == .ToJSON {
    var id = self.id
    id <- map["id"]
}
else {
    id <- map["id"]
}

In this solution you don't need an extra variable and the id will be in your json

from objectmapper.

dmorrow avatar dmorrow commented on July 26, 2024 9

Using the clone functionality in Realm, you can make this work

let preferencesCopy: Preferences = Preferences(value: preferences)
preferencesCopy.toJSON()

from objectmapper.

tristanhimmelman avatar tristanhimmelman commented on July 26, 2024

Hey there,

Weird issue indeed. It took me a little while to track down...

The custom operator that ObjectMapper uses is used for both Serialization and Deserialization of JSON. In deserialization the object on the left of the operator is being modified but not in serialization. Because of this the object being passed on the left side of the operator gets the flag "inout" in the operator function definition. This tells the compiler that the variable is modifiable within the scope of the function. This seems to be what Realm is unhappy about even though the object is not being modified during serialization. When I removed the flag and the deserialization code, the crash no longer happens...

I can't think of a solution off the top of my head to solve this issue. I will put some more thought into it to see if I can come up with a solution.

Tristan

from objectmapper.

zocario avatar zocario commented on July 26, 2024

Hi @tristanhimmelman, I am having the exactly same issue in my project.
During serialization of my Realm objects the App crashes as Realm thinks I am modifying the object when the mapping function is called.

As the issue is closed I guess you've found a solution for this problem?
I am using Xcode 6.3 beta with Swift 1.2, so I am on the "swift-1.2" branch, maybe the fix is not on it?

Enzo

from objectmapper.

tristanhimmelman avatar tristanhimmelman commented on July 26, 2024

Hi @zocario, no unfortunately I have not come up with a solution to this problem yet.

from objectmapper.

zocario avatar zocario commented on July 26, 2024

Did you try to talk with Realm guys to find a solution?
@ldiqual Have you made an issue in the Realm-Cocoa repository about this?

from objectmapper.

tristanhimmelman avatar tristanhimmelman commented on July 26, 2024

No I have not contacted them.

I believe the crash occurs because ObjectMapper defines the <- functions using the inout flag. For example:

public func <- <T>(inout left: T, right: Map)

This function is used both for parsing and writing JSON, yet the inout requirement is only necessary when parsing the JSON. Unfortunately I haven't come up with a nice way to split up the mapping functions so that inout isn't present when writing JSON.

from objectmapper.

zocario avatar zocario commented on July 26, 2024

Thanks for your precisions, I've just created and issue on the Realm repository to see if they have any suggestions that could be helpful to solve this problem.

from objectmapper.

segiddins avatar segiddins commented on July 26, 2024

You're correct in the guess that the issue is the spurious inout flag.

from objectmapper.

zocario avatar zocario commented on July 26, 2024

As I said in the Realm issue, I'll go with Mantle because I need a full compatibility with Realm, and solving this inout flag issue would mean change the design of your library...
Thanks for your support.

from objectmapper.

empty avatar empty commented on July 26, 2024

I'm also running into this issue. It would be great if ObjectMapper was compatible with Realm.

from objectmapper.

schmidan avatar schmidan commented on July 26, 2024

[edit] Ha, just now saw this comment/solution #294 [/edit]

If anyone runs into this again. We adopted a quick work-around until we move to another solution:

import ObjectMapper
class Event: Object, Mappable {
    dynamic var mappingIdentifier = "EVENT_NO_ID" {
        didSet {
            realmIdentifier = mappingIdentifier
        }
    }
    dynamic var realmIdentifier = "EVENT_NO_ID"

    func mapping(map: Map) {
       mappingIdentifier <- map["id"]
       ...
    }

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

We just make sure, that the mapped property isnt the realms primary key.
ugly but in our case so far functional.

from objectmapper.

felipowsky avatar felipowsky commented on July 26, 2024

Workaround:

func mapping(map: Map) {
        var opened = false
        if let realm = self.realm where !realm.inWriteTransaction {
            realm.beginWrite()
            opened = true
        }
        defer { if opened { map.mappingType == .FromJSON ? try! self.realm?.commitWrite() : self.realm?.cancelWrite() } }

        self.name <- map["name"]
        // ...
}

It should be used with caution because it might commit implicit changes to your database.

from objectmapper.

geodesicer avatar geodesicer commented on July 26, 2024

the workaround I use is the following:

class RBase: Object, Mappable {
dynamic var id = NSUUID().UUIDString
dynamic var idz = "" {
didSet {
idz = id
}
}

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

func mapping(map: Map) {
    ....
    idz <- map["id"]
    idz <- map["idz"]
}

Also see the complete code in Subclassing and Realm #462

from objectmapper.

morgz avatar morgz commented on July 26, 2024

I get issues with any Realm backed attribute not just the primary key because the <- operator makes realm think we're modifying the underlying object. Shame, I like objectMapper DSL but for JSON serialising a Realm object it appears to be pretty incompatible without starting a write transaction.

from objectmapper.

geodesicer avatar geodesicer commented on July 26, 2024

So far I have only seen that problem when when modifying the "id"-property, and we write a new JSON-file at most property changes due to specific requirements, i.e. using two different database engines and using the JSON-file as a "secure" transport mechanism between databases.

from objectmapper.

nabbestemmia avatar nabbestemmia commented on July 26, 2024

Thank you guys, I thought to be alone in this situation!

from objectmapper.

alexanderkhitev avatar alexanderkhitev commented on July 26, 2024

@thacilima Hello!
I use your code in Swift 3, but I get error libc++abi.dylib: terminating with uncaught exception of type NSException when I call the let JSON = Mapper().toJSON(messageModel)

MessageModel snippet code

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

    func mapping(map: Map) {
        if map.mappingType == .toJSON {
            var id = self.id
            id <- map["id"]
        } else {
            id <- map["id"]
        }
//        id <- map["id"]
        date <- map["date"]
        message <- map["message"]
        imageData <- map["imageData"]
        imageURL <- map["imageURL"]
        senderID <- map["senderID"]
        conversationID <- map["conversationID"]
        isIncoming <- map["isIncoming"]
        isNew <- map["isNew"]
        messageStatus <- map["messageStatus"]
    }

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

from objectmapper.

juanrodriguezarc avatar juanrodriguezarc commented on July 26, 2024

try using a realm transaction and don't use a primary key
let realm = try! Realm() try! realm.write({ print("'HERE'",Mapper().toJSONString(user!, prettyPrint: true)) })

from objectmapper.

liangsay avatar liangsay commented on July 26, 2024

i try using this one,it's OK, you can try
https://github.com/APUtils/ObjectMapperAdditions
image

from objectmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.