Comments (20)
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.
Using the clone functionality in Realm, you can make this work
let preferencesCopy: Preferences = Preferences(value: preferences)
preferencesCopy.toJSON()
from objectmapper.
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.
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.
Hi @zocario, no unfortunately I have not come up with a solution to this problem yet.
from objectmapper.
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.
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.
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.
You're correct in the guess that the issue is the spurious inout
flag.
from objectmapper.
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.
I'm also running into this issue. It would be great if ObjectMapper was compatible with Realm.
from objectmapper.
[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.
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.
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.
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.
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.
Thank you guys, I thought to be alone in this situation!
from objectmapper.
@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.
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.
i try using this one,it's OK, you can try
https://github.com/APUtils/ObjectMapperAdditions
from objectmapper.
Related Issues (20)
- Array of Dictionary mapping
- check the value for parse HOT 2
- XCODE 12.0 Could not find module 'ObjectMapper' for target 'arm64-apple-ios-simulator'; HOT 3
- keep the original order mapping and array
- Eventual crashing HOT 2
- Object converting to JSON is not working properly (Object.toJSON() or Object.toJSONString(prettyPrint: true))
- Module compiled with Swift 5.2.4 cannot be imported by the Swift 5.5.1 compiler, Xcode 13.1 Error
- hope support the "|" separator
- Undefined vs. nil HOT 1
- Map.subscript(key:nested:delimiter:ignoreNil:). crash
- Privacy manifest HOT 4
- Not working in Xcode 15 HOT 3
- Add Privacy Manifest HOT 2
- XCode 15 and Swift5 Error Please Update the Library to Swift 5
- Cannot install via SPM, error: Invalid manifest HOT 7
- pod not found 4.3.0 HOT 1
- // swift-tools-version:5.0 doesn't work under Xcode 15 HOT 4
- 4.4.X minimum version iOS 17.0 HOT 1
- Current Package.swift config does not work HOT 1
- How to use Generic,like HandyJSON?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objectmapper.