Unrealm icon indicating copy to clipboard operation
Unrealm copied to clipboard

How to update only some properties of record?

Open NortromInsanlyDev opened this issue 3 years ago • 4 comments

How can I update only some properties with keeping old other properties? Seems like Realm.create(_ : value: update) is not implemented. Thanks for this awesome repo.

NortromInsanlyDev avatar Jun 04 '21 03:06 NortromInsanlyDev

Can you bring an example of what you want to achieve?

arturdev avatar Jun 05 '21 18:06 arturdev

Not sure why @NortromInsanlyDev did not supply one but I'll give it a go.

struct myObject { var name: String var age: Int var favPlaces:[String] }

I just want to update the favPlaces array without having to supply the entire object.

justdan0227 avatar Aug 26 '21 22:08 justdan0227

Imagine:

var myObj = realm.objects(ofType: MyObject.self).last!

if you update only one property: myObj.favPlaces = ["SF", "LA"]

and save it to the realm by

try! realm.write {
    realm.add(myObj)
}

It will keep the old properties unchanged

arturdev avatar Aug 27 '21 12:08 arturdev

@arturdev I think a better example is I have

struct Parent {
    var name: String
    var superPower: String
   var children: [Child] 
}

struct Child {
  var name: String
  var talent: String
 var grandchildren: [Grandchild]
}

struct Grandchild {
  var age: Int
}

Given these data structures, say from my server I fetched an updated list of all of my parents' names and superPowers but didn't want to fetch all of the children and grandchildren because there was no modifications made to those objects. I'd like to have a way to update top-level properties of an array of objects and ignore the relations for those objects. your suggestion is that I have to first fetch the parent objects individually and update each individual property which is not very optimal.

jlott1 avatar Mar 21 '22 04:03 jlott1