You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Realm Objects with Date variables will never use JSONEncoder.dateEncodingStrategy parameter when not implementing encode(to encoder:) method at encoding time
#8425
Closed
rursache opened this issue
Nov 28, 2023
· 2 comments
· Fixed by #8443
I'm trying to encode my Realm Objects so they can be sent to a backend API.
My objects contain multiple Data variables. These should be in a fixed specified format when reaching the API however the resulting Data/JSON String will always encode the dates using timeIntervalSinceReferenceDate instead of the custom JSONEncoder parameters.
However, it works fine the moment I implement encode(to encoder:) method in said object. I would like to not add this amount of boilerplate code to each model as our app have multiple and large objects. This should not be required because Codable objects work as expected, just not with the Object inheritance.
I can reproduce this everytime even on a sample project.
Stacktrace & log output
No crashes or logs showing the issue
Can you reproduce the bug?
Always
Reproduction Steps
Models.swift:
structSyncResponseModel:Codable{letlocation:SyncLocationDataModel}structSyncLocationDataModel:Codable{letworkouts:[MyObject]}classMyObject:Object,Codable{@Persisted(primaryKey: true)varid:UUID@PersistedvartestDate:DateenumCodingKeys:String,CodingKey{case id, testDate
}// the issue is gone if i uncomment this://// func encode(to encoder: Encoder) throws {// var container = encoder.container(keyedBy: CodingKeys.self)// try container.encode(id, forKey: .id)// try container.encode(testDate, forKey: .testDate)// }}
ViewController.swift:
privatevardemoId:UUID!overridefunc viewDidLoad(){
super.viewDidLoad()self.writeObject()}overridefunc viewDidAppear(_ animated:Bool){
super.viewDidAppear(animated)self.runTests()}privatefunc writeObject(){RealmManager.shared.deleteAllObjects()// `RealmManager` is a manager class that communicates with realmletmodelTest=MyObject()//
modelTest.testDate =Date()RealmManager.shared.addObject(object: modelTest, update:.all)self.demoId = modelTest.id
}privatefunc runTests(){
guard let modelTest =RealmManager.shared.getObject(type:MyObject.self, uuid:self.demoId)else{fatalError()}letmyObjectUnmanaged=MyObject(value: modelTest)letlocation=SyncLocationDataModel(workouts:[myObjectUnmanaged])do{letencoder=JSONEncoder()// this is never called:
encoder.dateEncodingStrategy =.custom {(date, encoder)inprint("\n dateEncodingStrategy called \n")letformattedDate=Utils.shared.dateFormatter(format:"yyyy-MM-dd").string(from: date)varcontainer= encoder.singleValueContainer()try container.encode(formattedDate)}// alternatively, this is never used:// encoder.dateEncodingStrategy = .formatted(Utils.shared.dateFormatter(format: "yyyy-MM-dd"))letjsonString=tryString(data: encoder.encode(location), encoding:.utf8)// the `MyObject.testDate` here is `722892731.604744` instead of the `yyyy-MM-dd` formatprint(jsonString ??"no json")}catch{print(String(describing: error))}}
rursache
changed the title
Realm Objects with Date variables will never use JSONEncoder.dateEncodingStrategy parameter when not implementing encode(to encoder:) method
Realm Objects with Date variables will never use JSONEncoder.dateEncodingStrategy parameter when not implementing encode(to encoder:) method at encoding time
Nov 28, 2023
How frequently does the bug occur?
Always
Description
I'm trying to encode my Realm Objects so they can be sent to a backend API.
My objects contain multiple
Data
variables. These should be in a fixed specified format when reaching the API however the resultingData
/JSON String will always encode the dates usingtimeIntervalSinceReferenceDate
instead of the custom JSONEncoder parameters.However, it works fine the moment I implement
encode(to encoder:)
method in said object. I would like to not add this amount of boilerplate code to each model as our app have multiple and large objects. This should not be required becauseCodable
objects work as expected, just not with theObject
inheritance.I can reproduce this everytime even on a sample project.
Stacktrace & log output
No crashes or logs showing the issue
Can you reproduce the bug?
Always
Reproduction Steps
Models.swift
:ViewController.swift
:The resulting
jsonString
looks like this:Notice how
testDate
is aTimeInterval
instead of being a2023-11-28
Version
10.44.0
What Atlas Services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
iOS 17.1 | Swift 5.9
Build environment
Xcode version: 15.0.1 (15A507)
Dependency manager and version: Swift Package Manager
The text was updated successfully, but these errors were encountered: