objectbox / objectbox-swift

Swift database - fast, simple and lightweight (iOS, macOS)
https://swift.objectbox.io
Apache License 2.0
467 stars 30 forks source link

Make PropertyQuery consider the Query order #41

Open adgvcxz opened 4 years ago

adgvcxz commented 4 years ago

Version : v1.3.1

Description: When I use the PropertyQuery method to query, the sorting is incorrect

// objectbox: entity
final class Item {
    var id: Id = 0
    var data: Int64 = 0
    var created: Int64 = 0
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let store = try! createStore()
        let box: Box<Item> = store.box()
        putData(box: box)
        let build = try! box.query().ordered(by: Item.created).build()
        let values = try! build.property(Item.data).find()
        let times = try! build.property(Item.created).find()

        print(values)
        print(times)

    }

    private func putData(box: Box<Item>) {
        let value: [Int64] = [64087, 56654, 427, 95156, 75358]
        let times: [Int64] = [1596884723268, 1596884598790, 1596883589545, 1596880357258, 1596878457071]
        for i in 0..<value.count {
            let item = Item()
            item.created = times[i]
            item.data = value[i]
            try! box.put(item)
        }
    }
}

private func createStore() throws -> Store {
    let directory = try FileManager.default.url(
        for: .applicationSupportDirectory,
        in: .userDomainMask,
        appropriateFor: nil,
        create: true
    ).appendingPathComponent("Test")
    try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false, attributes: nil)
    return try Store(directoryPath: directory.path)
}

Output:

[64087, 56654, 427, 95156, 75358]

[1596884723268, 1596884598790, 1596883589545, 1596880357258, 1596878457071]
greenrobot commented 4 years ago

This is a documented limitation:

Currently, property queries do not honor the ordered(by:,flags:) setting. If you need a fixed order for your proerty query results, you must sort them manually after retrieving them.

I'm changing this issue to a feature request.