realm-swift: setValue forKey not working with List
I have an object in Realm. I would like to retrieve that object and then work with an unmanaged version of that object. Unfortunately, it doesn’t seem like Realm for iOS has any good copy options, so I tried to followed the “detachable” workaround by “anlaital” outlined in issue #3381.
My code is as follows:

This works for the most part. When on a property that is a list and has values, detachable.detached() does return the expect list copy. Unfortunately, detached.setValue(detachable.detached(), forKey: property.name) does not set the value for any Lists my object has.

Any thoughts on how I can properly assign my “detached”/unmanaged list to my object?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 17 (4 by maintainers)
I actually have a slightly newer version of this.
The documentation is either unclear or wrong. I think the original intent was to say you can modify the elements inside a
List, including assigning new objects to a particular index, rather than assigning a newListinstance altogether. I’ll update it, thanks for bringing it to our attention.Hi, can someone translate @Alarson93’s solution in Objective C.
@allenhumphreys Comment on my other issue (#5469) gave me a magnificent idea. Instead of having the messy casting for lists like I do in post 4 (which incidentally crashes for objects), because this is a detached/unmanaged object, I should be able to safely do setValueForKey for lists.
I tested this and it seemed to work, but I would like to hear your thoughts @austinzheng. Do you think it is safe to set/assign a list in this manner on the premise that everything is unmanaged?
For what it’s worth, I’ve had success with this approach:
I’m appending to lists (reference grabbed via getValue:forKey: ). The unfortunate part is I must be quite verbose when checking list types (note the
as? List<Int>,as? List<String, and so on).