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

Crash on property/distinct query #36

Closed
pfvernon2 opened this issue Apr 30, 2020 · 7 comments
Closed

Crash on property/distinct query #36

pfvernon2 opened this issue Apr 30, 2020 · 7 comments
Milestone

Comments

@pfvernon2
Copy link

Xcode 11.4.1, Swift 5.x, iOS 13.4.1

I'm seeing a general protection fault when attempting a property/distinct query. This is occurring both on hardware and in the simulator.

I've replicated this crash in the following trivial example.

class ExampleEntity: Entity {
    var id: Id = 0

    // objectbox: index
    var test: String = ""
}

func test() {
    do {
        //Boilerplate… create store and open box
        let databaseName = "notes"
        let appSupport = try FileManager.default.url(for: .applicationSupportDirectory,
                                                     in: .userDomainMask,
                                                     appropriateFor: nil,
                                                     create: true)
            .appendingPathComponent(Bundle.main.bundleIdentifier!)
        let directory = appSupport.appendingPathComponent(databaseName)
        try FileManager.default.createDirectory(at: directory,
                                                withIntermediateDirectories: true,
                                                attributes: nil)
        let store = try Store(directoryPath: directory.path)
        let exampleEntityBox = store.box(for: ExampleEntity.self)

        //if box is empty add some random values
        var boxCount = try exampleEntityBox.count()
        if boxCount == 0 {
            let testCases = ["foo", "bar", "foobar", "barfoo"];
            for _ in 1...100 {
                let exampleEntity = ExampleEntity()
                exampleEntity.test = testCases.randomElement() ?? "default"
                try exampleEntityBox.put(exampleEntity)
            }
            boxCount = try exampleEntityBox.count()
        }

        assert(boxCount > 0)

        //get distinct values the hard way
        let all = try exampleEntityBox.all()
        let testSet = Set(all.map { $0.test })
        print(testSet)

        //MARK: Crash - general protection fault
        //get distinct values the objectbox way
        let distinct: [String] = try exampleEntityBox.query().build()
            .property(ExampleEntity.test)
            .distinct(caseSensitiveCompare: false)
            .findStrings()

        print(distinct)

    } catch {
        print("error")
    }
}

Am I doing something incorrectly? Is this a known issue?

@greenrobot
Copy link
Member

A "general protection fault" should never happen, looking into it...

@greenrobot
Copy link
Member

Reproduced! Thank you for the example code! 👍

If you follow the example of the property query Swift docs and assign the query to a variable, it should work.

If you put everything in one line, the main query gets discarded and thus crashes. Will check now if we can prevent this.

@greenrobot greenrobot added this to the 1.3.0 milestone May 10, 2020
@greenrobot
Copy link
Member

Improved internally, will be part of next release (1.3).

@pfvernon2
Copy link
Author

Thank for you looking into the issue and working on a fix!

I have confirmed that creating the query as a variable before executing the property/distinct lookup works as expected:

//Does NOT crash!
let query = try exampleEntityBox.query().build()
let distinct: [String] = try query.property(ExampleEntity.test).distinct(caseSensitiveCompare: false).findStrings()
print(distinct)

I could not find an example of a property/distinct query in your documentation that was structured in this way, however. The one example I could find was expressed as a single line as I did above. I mention this only because you may want to update your documentation until a fix can be released.

From the ObjectBox Swift documentation:

image

@greenrobot
Copy link
Member

Thanks for pointing to those docs... 🤔

If you know how, you could try this out now using https://github.com/objectbox/objectbox-swift-spec-staging/releases/tag/v1.3.x.

If not, the official release comes soon.

@pfvernon2
Copy link
Author

Looks good! No more crash using the single line form in my trivial example project.

Thanks again for the quick fix.

@greenrobot
Copy link
Member

1.3.0 has been released with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants