objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

Query missing one record and invalid field assignment during read operation #550

Closed lberbey closed 8 months ago

lberbey commented 9 months ago

Hi guys,

First of all, sorry for my english (I'm french).

I have two issues on the same project :

Sorry if I'm wrong. Your product is really good and quite easy to use. I'm really happy with it. I tried to understand sub classes process to fix this but guys, I'm just a simple developer and all the work on ByteArray, Buffer... is far to complex for me 👼 .

Basic info:

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.2) • Android SDK at C:\AndroidSDK • Platform android-33-ext4, build-tools 33.0.2 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-9505619) • All Android licenses accepted.

[√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.24) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.11.33328.57 • Windows 10 SDK version 10.0.20348.0

[√] Android Studio (version 2022.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-9505619)

[√] VS Code, 64-bit edition (version 1.80.1) • VS Code at C:\Program Files\Microsoft VS Code • Flutter extension version 3.68.0

[√] Connected device (4 available) • M2002J9G (mobile) • 1eabc81d • android-arm64 • Android 12 (API 31) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [version 10.0.19045.3086] • Chrome (web) • chrome • web-javascript • Google Chrome 115.0.5790.110 • Edge (web) • edge • web-javascript • Microsoft Edge 115.0.1901.188

[√] Network resources • All expected network resources are available.

• No issues found!


### Steps to reproduce

1. Load the project from [here](https://gitlab.com/loic.berbey/objectbox_issue/-/tree/master?ref_type=heads)
2. Add some breakpoints

main.dart l.187 main.dart l.191 main.dart l.194 objectbox.g.dart l.465 objectbox.g.dart l.469


4. Execute the project (all breakpoints will be traversed during main.dart execution)

### Expected behavior

1. On `main.dart l.191` **recipes** list should be equal to **recipes** list on `main.dart l.187` like it worked for **shops** list just above
2. On `main.dart l.194` **days[0]** should contains 1 item in **lunchMenu** and 2 in **dinnerMenu** properties

### Code

- `objectbox.g.dart l.465` : value seems available before dbLunchMenu assignment
![objectbox - objectFromFB - before dbLunchMenu](https://github.com/objectbox/objectbox-dart/assets/4833042/acfcd25a-f257-47c1-96d6-95294efefd6e)
- `objectbox.g.dart l.469` : value is unavailable after dbLunchMenu assignment
![objectbox - objectFromFB - after dbLunchMenu](https://github.com/objectbox/objectbox-dart/assets/4833042/95e9d96e-9b94-4ca1-9537-2e891e9bf4d7)

### Additional context

- I find one workaround for the field assignment during read operation : replace properties `lunchMenu` and `dinnerMenu` by `menu` and `dbLunchMenu` and `dbDinnerMenu` by `dbMenu` but I'm not shure why is it working 😭.

Thanks for your time 🍻 .
greenrobot-team commented 9 months ago

Thanks for reporting, I could verify your project fails as expected.

Things I should look into:

lberbey commented 9 months ago

Hi,

Thanks for answering that fast.

For your second point, I believe the issue is on read and not on write (or maybe on both...). As you can see in my first capture (original post), I've added the assignation of dbLunchMenu and dbDinnerMenu to WATCH view. Values of the two lists seems to be accessible before the assignation of dbLunchMenu.

I've tried adding a new property List<String> dbZZZ which is read and loaded between dbLunchMenu and dbDinnerMenu. Same results and maybe worst : Both dbZZZ and dbDinnerMenu aren't assigned.

objectbox - objectFromFB - after dbLunchMenu + new dbZZZ property

I tried to switch properties assignment order and it seems to do something : with the order dbZZZ, dbDinnerMenu, dbLunchMenu and dbZZZ equal to ['test'], dbDinnerMenu is also set. Even with dbZZZ equals to [db.generateString(300), db.generateString(700)] it worked.

greenrobot-team commented 9 months ago

I could isolate the issue: the problem is that as part of setting some properties a database query is executed. This query will run while an object is getting read. This will cause wrong values to be read.

In your code look at the RecipeIngredient.fromDB factory. Removing the database queries there fixes your reported issues.

I still need to investigate if this is something that should work or should throw an error.

lberbey commented 9 months ago

Thank you for the work. I understand why I wasn't able to reproduce it only with String.

Correct me if I'm wrong : If I had create all the missing objects (Section, RecipeIngredient...) with the proper ToOne/ToMany relations it would be ok. My issue is all about having one or many read operations during another one.

For my own good, I hope you will improve the tool but I would understand if you don't. I guess it's a lot of work and the community doesn't really need it, otherwise it would have been brought up sooner.

greenrobot-team commented 9 months ago

We have released version 2.2.0 which should resolve this issue. Please update using flutter pub upgrade (or dart pub upgrade for Dart Native projects).

lberbey commented 9 months ago

Thanks you for the update. Does the new version fix both issues (query and read operation) ?

I've updated my sandbox project as requested and recipes = db.getManyByUuid(... on main.dart l.187 stills returns me 1 object. For the other issue I can confirm that days[0] is now filled with the appropriate values.

greenrobot-team commented 9 months ago

Thanks! I just verified and this is true. The issue only appears when running a query as part of constructing an object and then querying for those objects.

The query then will only return one object, regardless of how many objects are in the box.

I need to have another look at this.

greenrobot-team commented 8 months ago

We have released version 2.2.1 which I verified resolves this issue with the example project you have given. Please update using flutter pub upgrade (or dart pub upgrade for Dart Native projects).

Thanks again!

lberbey commented 8 months ago

Many thanks for your work and time. Everything is fine !