Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with ObjectMapper #1726

Closed
zocario opened this issue Apr 3, 2015 · 5 comments
Closed

Issues with ObjectMapper #1726

zocario opened this issue Apr 3, 2015 · 5 comments

Comments

@zocario
Copy link

zocario commented Apr 3, 2015

I am using Realm on my project (using Swift 1.2) with ObjectMapper to serialize and deserialize JSON Objects I receive and send to my REST Api.
Deserialization of objects works fine, but when I want to serialize one of my Realm object, I am having crashes like this:

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

Or this if I don't include the primary key of the objects

*** Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

My Realm objects implement the Mappable protocol of ObjectMapper

func mapping(map: Map) {
    id <= map["id"]
    name <= map["name"]
    about <= map["about"]
    goal <= map["goal"]
}

I was really surprised to have this kind of error, as Realm acts as if I was editing my objects but I am just creating a JSON dictionary to send them to my API using this code:

let json = Mapper<MyClass>().toJSON(self)

This is a known issue on the ObjectMapper repository.
It seems the problem is the custom operator used by ObjectMapper <= that makes Realm thinks we want to edit the fields as they are passed as inout parameters:

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

But this operator, depending on the mapping type (fromJSON or toJSON), will not always edit the inout field. When we are in a "toJSON" case, it'll only read its value and put it in the new JSON dictionary.

The creator of ObjectMapper hasn't find a solution for the moment, and I was wondering if you have any suggestions or pending changes that could help to solve this problem?

@segiddins
Copy link
Contributor

Hi @zocario , do you have a project that you can share that we can use to reproduce the issue?

@zocario
Copy link
Author

zocario commented Apr 4, 2015

Hi @segiddins, check this demo project that crashes with this issue.
The code that crashes is in theviewDidLoad of the ViewController class.

@segiddins segiddins removed the pending label Apr 7, 2015
@segiddins segiddins self-assigned this Apr 7, 2015
@segiddins
Copy link
Contributor

@zocario so, the issue there is that it's using an inout parameter. In Swift, inout means that the variable at the callsite is reassigned every time, even if there are no changes. I'd lean towards saying this is an issue with the design of ObjectMapper.

@zocario
Copy link
Author

zocario commented Apr 8, 2015

Thanks @segiddins for your answer.
I'll go with Mantle instead of ObjectMapper as there is no easy fix to get ObjectMapper working fine with Realm.

@dmorrow
Copy link

dmorrow commented Jan 31, 2017

I solved this issue by making a clone of the object I wanted to transform to JSON

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants