Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm 5.0.0 filter not returning correct results #6540

Closed
Jaycyn opened this issue May 28, 2020 · 1 comment · Fixed by realm/realm-core#3752
Closed

Realm 5.0.0 filter not returning correct results #6540

Jaycyn opened this issue May 28, 2020 · 1 comment · Fixed by realm/realm-core#3752

Comments

@Jaycyn
Copy link

Jaycyn commented May 28, 2020

Issue

Realm 5.0.0 filter not returning correct results when filtering on an objects List property.

Goals

Return the correct results

Expected Results

When filtering and there are 3 objects that match the filter, the results of the filter should contain 3 objects.

Actual Results

Sometimes the filter will return 1 result, other times two results, and rarely return all three results

Steps to Reproduce

A project with three Realm objects Account, Transaction and Item.

In brief:

AccountClass
  @objc dynamic var account_id = 1
   let transactionList = List<TransactionClass>()

TransactionClass
   @objc dynamic var account: AccountClass!

ItemClass
  let transactions = List<TransactionClass>()
  func printResultsCount() {
     let results = self.transactions.filter("account.account_id == 1")
     print(results.count) //sometimes prints 1, sometimes 2 and occasionally 3.
  1. Create one AccountClass object, ensuring it's account_id = 1
  2. Then we create three transactions and set their account property to that account class object
  3. Lastly append the three transaction to the item's transactions list

The goal is to query the Item for all transactions where the transaction account number is 1

So as you can see we have a function in the item that should query for all transactions whose account property is an account where account_id = 1.

Sometimes the count is 1, sometimes 2, sometimes 3.

However, if we use a filter directly on the transactions, it outputs correctly

myButtonAction {
   let results = realm.objects(TransactionClass.self).filter("account.account_id == 1")
   print(results)
}

and will output 3 every time.

Code Sample

class AccountClass: Object {
    @objc dynamic var account_id = 0
    @objc dynamic var account_name = ""
    
    let transactionList = List<TransactionClass>()
    override static func primaryKey() -> String {
        return "account_id"
    }
}

class TransactionClass: Object {
    @objc dynamic var transaction_id = UUID().uuidString
    @objc dynamic var transaction_name = ""
    @objc dynamic var account: AccountClass!

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

class ItemClass: Object {
    @objc dynamic var item_id = UUID().uuidString
    @objc dynamic var item_name = ""

    let transactions = List<TransactionClass>()
    
    func printTransactions()  {
        self.transactions.forEach { trans in
            let id = trans.transaction_id
            let acct = trans.account.account_name
            let name = trans.transaction_name
            print(id, acct, name)
        }
    }

   func printFilteredResults() {
      let results = self.transactions.filter("account.account_id == 1")
      print(results.count) //1, sometimes 2, sometimes 3
   }
   
    override static func primaryKey() -> String {
        return "item_id"
    }
}

The two functions in the item class are just to for output testing. printTransactions prints the three transactions and the printFilteredResults should print the number of transactions that match the query.

Version of Realm and Tooling

Realm framework version: 5.0.0
Realm Object Server version: N/A
Xcode version: 11.3.1
iOS/OSX version: 10.14.x and 10.15.x
Dependency manager + version: N/A

@ejm01
Copy link
Contributor

ejm01 commented May 29, 2020

Thanks for the detail. I believe I've been able to replicate this and still looking into this further.

@tgoyne tgoyne self-assigned this May 29, 2020
@tgoyne tgoyne added the T-Bug label May 29, 2020
tgoyne added a commit to realm/realm-core that referenced this issue May 29, 2020
The optimization added in #3432 to use the search index when performing
equality queries over a link assumed that Queries would be evaluated in
ascending table order, which is not always the case when the query is being
performed on a link list.

Fixes realm/realm-swift#6540.
tgoyne added a commit to realm/realm-core that referenced this issue May 29, 2020
The optimization added in #3432 to use the search index when performing
equality queries over a link assumed that Queries would be evaluated in
ascending table order, which is not always the case when the query is being
performed on a link list.

Fixes realm/realm-swift#6540.
jedelbo added a commit to realm/realm-core that referenced this issue Jun 2, 2020
…er (#3752)

The optimization added in #3432 to use the search index when performing
equality queries over a link assumed that Queries would be evaluated in
ascending table order, which is not always the case when the query is being
performed on a link list.

Fixes realm/realm-swift#6540.

* Remove an unused member variable

Co-authored-by: Jørgen Edelbo <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants