objectbox / objectbox-dart

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

What's causing the second else? (set target) #285

Closed Katekko closed 3 years ago

Katekko commented 3 years ago

I'm doing this: image

And every time is reaching the second else, doing my target getting null. That is something else that I need to do?

image

vaind commented 3 years ago

Looks good to me. The third branch (...unknown()) says it is not known whether this is a new object (id == 0) or an existing one (id != 0), because it has no way to find out the ID yet, if the store hasn't been attached to this relation. This is all just fine and the only limitation is you cannot access toUserProfileEntity.user.targetId yet, i.e. read the ID - same as above, it can't find out.

When you call put on the base object, for example by box.put(toUserProfileEntity), the model is made known to the ToOne relation and it finds out the ID is zero and puts the target as well, transparently. Afterwards toUserProfileEntity.user.targetId also starts working as expected, returning a non-zero ID of the just inserted UserEntity.

To help me understand your situation, could you maybe elaborate why reaching that third branch looks like an issue to you? Does it cause you any problems in your app?

BTW if you really wanted, you could call toUserProfileEntity.user.attach(store) manually before setting the target, though that's very rarely needed (as I've said above, only if you require toUserProfileEntity.user.targetId to work before you put(toUserProfileEntity).

Katekko commented 3 years ago

Right, I will try to show step by step what is happening with me:

First: I set the target image

In this step, the strange for me is, even the target was set the attribute is null; image image

Second: I save(put) the object (I guess with the target) and get the ID back image

Third: I'm trying to get again the object saved to see the relation, but without a success. image

I can uploud my code for you if you want to. Its a opensource project that I working on to create some clean architeture easy to understand.

vaind commented 3 years ago

I can uploud my code for you if you want to. Its a opensource project that I working on to create some clean architeture easy to understand.

that would be the most efficient I guess - just let me know what to run

Katekko commented 3 years ago

I can uploud my code for you if you want to. Its a opensource project that I working on to create some clean architeture easy to understand.

that would be the most efficient I guess - just let me know what to run

https://github.com/Katekko/github-clean

You will need two things:

  1. Copy and paste the config.example.dart and change the name to config.dart

  2. Generate one access token to access the github API. That's no need any permission, the app is just to search and list the github users.

Tell me if you get any problem to run.

Now to reproduce the problem that is happening to me:

  1. Go to user_profile.model.dart and break point the line 77
  2. Open the app, will load some github users, click in anyone, will open the profile
  3. Click on the heart to favorite an user, will trigger the save on file user_profile.model.dart.
vaind commented 3 years ago

that was a bit hard to debug.... you call this getter every time - and it creates a new object on every call, so the object you set .user.target on isn't the same one you save...

  UserProfileEntity get toUserProfileEntity {
    return UserProfileEntity(
      id: localProfileId,
      bio: bio,
      email: email,
      location: location,
      nickname: nickname,
    );
  }
Katekko commented 3 years ago

Thank you very much, awesome.

Idk how I missed this, thanks again

vaind commented 3 years ago

You're welcome

On Thu, Jul 15, 2021, 7:58 PM Katekko @.***> wrote:

Thank you very much, awesome.

Idk how I missed this, thanks again

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/objectbox/objectbox-dart/issues/285#issuecomment-880900671, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQOG4WNSQ3VPRGLYV6P6UDTX4OUJANCNFSM5ANYAGZA .