Save an Object which has 2 properties Date and List (Array).
The object should be saved in database with both property values i.e. date and List
The object get saved but only with one property, which is of type Date. The List property is not being saved in database.
class _ElectricityPricesModel: Object { // Object class to save
@objc dynamic var updated : Date?
var data = List<_PriceModel>()
}
class ElectricityPricesModel: _ElectricityPricesModel { // class responsible to save the data
static func savedElectricityPricesModel(realm: Realm?) -> ElectricityPricesModel! // app can only have one _ElectricityPricesModel
{
if realm == nil
{
return nil
}
let electricityPricesModel = realm?.objects(ElectricityPricesModel.self)
if electricityPricesModel?.first != nil
{
return electricityPricesModel?.first
}
let newElectricityPricesModel = ElectricityPricesModel()
try! realm?.write {
return realm?.add(newElectricityPricesModel)
}
return newElectricityPricesModel
}
static func updateIn(realm: Realm?, electricityPricesModel: EditableElectricityPricesModel?)
{
if electricityPricesModel == nil || realm == nil
{
return
}
let savedElectricityPricesModel = self.savedElectricityPricesModel(realm: realm)
try! realm?.write {
savedElectricityPricesModel?.data = electricityPricesModel!.data
savedElectricityPricesModel?.updated = electricityPricesModel!.updated
}
}
}
I call the function as follows:
ElectricityPricesModel.updateIn(realm: self.realmObject, electricityPricesModel: electricityPricesModel)
And in next cycle (when I try to save the object again) I check the value by using breakpoint ans it shows the following result:
Printing description of savedElectricityPricesModel:
â–ż Optional<ElectricityPricesModel>
- some : ElectricityPricesModel {
updated = 2017-12-11 14:00:00 +0000;
data = RLMArray<_PriceModel> <0x1095347e0> (
);
}
Realm framework version: 3.0.2
Realm Object Server version: ? --
Xcode version: 9.2
iOS/OSX version: 11.1.2
Dependency manager + version: COCOAPODS: 1.3.1
The declaration of your List<T> property should use let instead of var.
let data = List<_PriceModel>()
The compiler will now catch places where you attempt to assign to the data property rather than mutating it in place. This is necessary because Swift does not provide any means to intercept an assignment to a property whose type uses generics (more specifically, any type that is not representable in Objective-C).
For instance:
savedElectricityPricesModel?.data = electricityPricesModel!.data
Should instead be:
savedElectricityPricesModel?.replaceSubrange(0..<savedElectricityPricesModel.count, with: electricityPricesModel!.data)
I am still unclear about the solution, even after using let i am not getting my data persisted..
@kalrashubham49 What is your issue? Please explain in detail.
List Type in not getting persisted in Realm. While all other properties are getting saved.
Can you please paste some small a related code here?
class _ElectricityPricesModel: Object,Mappable { // Object class to save
@objc dynamic var updated : Date?
let data = List<_PriceModel>()
func mapping(map: Map)
{
updated <- map[“updated_at”]
data <- map[“models”]
}
}
class ElectricityPricesModel: _ElectricityPricesModel { // class responsible to save the data
static func savedElectricityPricesModel(realm: Realm?) -> ElectricityPricesModel! // app can only have one _ElectricityPricesModel
{
if realm == nil
{
return nil
}
let electricityPricesModel = realm?.objects(ElectricityPricesModel.self)
if electricityPricesModel?.first != nil
{
return electricityPricesModel?.first
}
let newElectricityPricesModel = ElectricityPricesModel()
try! realm?.write {
return realm?.add(newElectricityPricesModel)
}
return newElectricityPricesModel
}
static func updateIn(realm: Realm?, electricityPricesModel: EditableElectricityPricesModel?)
{
if electricityPricesModel == nil || realm == nil
{
return
}
let savedElectricityPricesModel = self.savedElectricityPricesModel(realm: realm)
try! realm?.write {
savedElectricityPricesModel?.data = electricityPricesModel!.data
savedElectricityPricesModel?.updated = electricityPricesModel!.updated
}
}
}
My Model is
Okay @kalrashubham49
replace savedElectricityPricesModel?.data = electricityPricesModel!.data
with -
```
for priceModel in electricityPricesModel!.data
{
savedElectricityPricesModel?.data.append(priceModel)
}
```
Let me know if you face any problem.
Still not working. for Transforming List, i have used 2 methods both of
them are ot working
1 - overloading Operation <-
func <-
{
var array: [T]?
if right.mappingType == .toJSON {
array = Array(left)
}
array <- right
if right.mappingType == .fromJSON {
if let theArray = array {
left.append(objectsIn: theArray)
}
}
}
2 - Transforming List from Array
class RealmListTransform
typealias Object = List<T>
typealias JSON = [[String:Any]]
let mapper = Mapper<T>()
func transformFromJSON(_ value: Any?) -> List<T>? {
let result = List<T>()
if let tempArr = value as? [Any] {
for entry in tempArr {
let mapper = Mapper<T>()
let model : T = mapper.map(JSONObject: entry)!
result.append(model)
}
}
return result
}
func transformToJSON(_ value: Object?) -> JSON? {
var results = [[String:Any]]()
if let value = value {
for obj in value {
let json = mapper.toJSON(obj)
results.append(json)
}
}
return results
}
}
On Fri, Jun 1, 2018 at 6:48 PM, Ankush Kushwaha notifications@github.com
wrote:
Okay @kalrashubham49 https://github.com/kalrashubham49
replace savedElectricityPricesModel?.data = electricityPricesModel!.data
with -
for priceModel in electricityPricesModel!.data
{
savedElectricityPricesModel?.data.append(priceModel)
}Let me know if you face any problem.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/realm/realm-cocoa/issues/5518#issuecomment-393877512,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AM989KnX7DJE6906_EoqPd94R-Iw-Wahks5t4T8WgaJpZM4Q--zp
.
I am having this issue, my list property does not show up in my Realm when I inspect it with Realm Studio. It is declared using 'let' as @bdash has said. @kalrashubham49 did you find a solution to your problem? Using XCode 11.2.1 and Realm Swift 3.21.0.
Most helpful comment
I am having this issue, my list property does not show up in my Realm when I inspect it with Realm Studio. It is declared using 'let' as @bdash has said. @kalrashubham49 did you find a solution to your problem? Using XCode 11.2.1 and Realm Swift 3.21.0.