Swift : How to merge two Realm Results in on in swift

425 views Asked by At

I am querying realm for two different keywords then I want to merge those two realm result into one so I loop through its.

I have this Object descendent class:

class Item: Object {
  @objc dynamic var _id: ObjectId? = nil
  @objc dynamic var _partitionKey: String = ""
  @objc dynamic var productBrand: String? = nil
  

  override static func primaryKey() -> String? {
      return "_id"
    }
 }

I'm fetching the realms

class SearchResult : ObservableObject {
      @Published var storeitems: Results<Item>
     
    storeitems = realm.objects(Item.self).sorted(byKeyPath: "_id") 

     let p1 = self.storeitems.filter("productDescription CONTAINS '"Cheese"'")
    let p2 = self.storeitems.filter("productDescription CONTAINS '"Blue"'")
}

I am trying to merge p1 and p2 to get something like

 p3 = p1 + p2

How can this be implemented with realm result?

1

There are 1 answers

0
JLM On

You can adjust your filter to get what you want in one go:


let items = storeitems.filter { item in 
    item.productDescription.contains("Cheese") || item.productDescription.contains("Blue")
}