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

Distinct query of a relationship returns a wrong result at the first time #7481

Closed
aspick opened this issue Oct 14, 2021 · 0 comments · Fixed by realm/realm-core#4968
Closed
Assignees

Comments

@aspick
Copy link

aspick commented Oct 14, 2021

How frequently does the bug occur?

All the time

Description

Distinct query of a relationship like

let breeds = person.dogs.sorted(byKeyPath: "breed").distinct(by: ["breed"])
breeds.count

returns a wrong result at the first time.

Expected result

It returns value less than person.dogs.count all the time.

Actual result

It returns value same as person.dogs.count at the first time.
And it returns right value after the second time.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

Replace examples/ios/swift/Simple/AppDelegate.swift with below codes and run it.

import UIKit
import RealmSwift

class Person: Object {
    @Persisted var name: String
    @Persisted var dogs: List<Dog>
}

class Dog: Object {
    @Persisted var name: String
    @Persisted var breed: String
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UIViewController()
        window?.makeKeyAndVisible()

        _ = try! Realm.deleteFiles(for: Realm.Configuration.defaultConfiguration)
        
        // data setup
        let person = Person()
        person.name = "Rowan"

        let realm = try! Realm()
        realm.beginWrite()

        [
            ["Rex",      "Labrador Retrieve"],
            ["Emerson",  "German Shepherd"],
            ["Corey",    "Golden Retriever"],
            ["Phoenix",  "Labrador Retrieve"],
            ["Campbell", "German Shepherd"]
        ].forEach { row in
            let dog = Dog()
            dog.name = row[0]
            dog.breed = row[1]
            realm.add(dog)
            person.dogs.append(dog)
        }
        realm.add(person)
        try! realm.commitWrite()
        
        // query
        let breeds = person.dogs.sorted(byKeyPath: "breed").distinct(by: ["breed"])
        print("breeds count (1st): \(breeds.count)")
        print("breeds count (2nd): \(breeds.count)")

        return true
    }
}

Version

10.17.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS14.5, iOS15.0

Build environment

ProductName:	macOS
ProductVersion:	11.6
BuildVersion:	20G165

/Applications/Xcode.app/Contents/Developer
Xcode 13.0
Build version 13A233

/Users/xxx/.rvm/gems/ruby-2.7.3/bin/pod
1.11.2
(not in use here)

/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)


(not in use here)

/usr/local/bin/git
git version 2.29.2
@aspick aspick added the T-Bug label Oct 14, 2021
@tgoyne tgoyne self-assigned this Oct 15, 2021
@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.

2 participants