realm-swift icon indicating copy to clipboard operation
realm-swift copied to clipboard

RCOCOA-2271: Collections in Mixed

Open dianaafanador3 opened this issue 10 months ago • 3 comments

  • Added support for storing nested collections (List and Map not ManagedSet) in a AnyRealmValue.

    class MixedObject: Object {
      @Persisted var anyValue: AnyRealmValue
    }
    
    // You can build a AnyRealmValue from a Swift's Dictionary.
    let dictionary: Dictionary<String, AnyRealmValue> = ["key1": .string("hello"), "key2": .bool(false)]
    
    // You can build a AnyRealmValue from a Swift's Map
    // and nest a collection within another collection.
    let list: Array<AnyRealmValue> = [.int(12), .double(14.17), AnyRealmValue.fromDictionary(dictionary)]
    
    let realm = realmWithTestPath()
    try realm.write {
      let obj = MixedObject()
      obj.anyValue = AnyRealmValue.fromArray(list)
      realm.add(o)
    }
    
  • Added new operators to Swift's Query API for supporting querying nested collections.

    realm.objects(MixedObject.self).where { $0.anyValue[0][0][1] == .double(76.54) }
    

    The .all operator allows looking up in all keys or indexes.

    realm.objects(MixedObject.self).where { $0.anyValue["key"].all == .bool(false) }
    

dianaafanador3 avatar Apr 12 '24 16:04 dianaafanador3

This is waiting for this PR to be merged to core. Also, there is one failing test which is working locally, taking a look at it, but putting this on review on the meantime.

dianaafanador3 avatar Apr 22 '24 11:04 dianaafanador3

@tgoyne and @leemaguire solved pr comments and there are some comments as well

dianaafanador3 avatar Apr 24 '24 14:04 dianaafanador3

There are a bunch of new warnings here that need to be fixed.

tgoyne avatar Apr 29 '24 19:04 tgoyne

@tgoyne solved PR comments and removed the warnings

dianaafanador3 avatar May 22 '24 23:05 dianaafanador3