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

ObservableMap not notifying Observer mobx 2.2.3 it was woking fine in 2.2.1 #969

Closed
boradparas opened this issue Dec 8, 2023 · 6 comments · Fixed by #971
Closed

ObservableMap not notifying Observer mobx 2.2.3 it was woking fine in 2.2.1 #969

boradparas opened this issue Dec 8, 2023 · 6 comments · Fixed by #971
Assignees
Labels
investigation Needs some research and investigation question Further information is requested

Comments

@boradparas
Copy link

I am using so many ObservableMaps in the app and none of them are updating the UI since I upgraded to 2.2.3

@observable ObservableMap<String, ParticipantExtended> sortedParticipants = ObservableMap<String, ParticipantExtended>();

Observer(
                          builder: (_) {
                            return SliverGrid(
                              gridDelegate:
                                  SliverGridDelegateWithFixedCrossAxisCount(
                                crossAxisCount: crossAxisCount,
                                childAspectRatio: tileWidth / tileHeight,
                                crossAxisSpacing: 2,
                                mainAxisSpacing: 2,
                              ),
                              delegate: SliverChildBuilderDelegate(
                                (context, index) {
                                  final participant = videoRoomStore
                                      .sortedParticipants.values
                                      .elementAt(index);
                                 return VisibilityDetector(
                                    key: Key(participant.identity),
                                    onVisibilityChanged: (info) {
                                      final bool isVisible =
                                          info.visibleFraction > 0;
                                      final bool isCompletelyGone =
                                          info.visibleFraction == 0;
                                      final bool isSubscribed = videoRoomStore
                                                  .participantSubscriptions[
                                              participant.identity] ??
                                          false;
                                      final bool shouldSubscribe =
                                          !isSubscribed &&
                                              isVisible &&
                                              !participant.isLocalParticipant;

                                      if (shouldSubscribe) {
                                        videoRoomStore.subscribeToVideoTracks(
                                          participant.participant
                                              as RemoteParticipant,
                                        );
                                        videoRoomStore.visibleParticipants[
                                            participant.identity] = true;
                                      } else if (!participant
                                              .isLocalParticipant &&
                                          isCompletelyGone) {
                                        videoRoomStore.unSubscribeToVideoTracks(
                                          participant.participant
                                              as RemoteParticipant,
                                        );
                                        videoRoomStore.visibleParticipants
                                            .remove(
                                          participant.identity,
                                        );
                                      }
                                    },
                                    child: ParticipantTile(
                                      participant: participant,
                                    ),
                                  );
                                },
                                childCount: videoRoomStore.isAppInForeground
                                    ? videoRoomStore.sortedParticipants.length
                                    : 0,
                              ),
                            );
                          },
                        ),

What change I need to make to keep it working as earlier it used to work?

@amondnet amondnet self-assigned this Dec 13, 2023
@amondnet
Copy link
Collaborator

amondnet commented Dec 13, 2023

@boradparas
The issue may be related to this PR. Since 2.2.3, mobx checks for equality of map, iterable with DeepCollectionEquality.

Could you try one of the following?

  1. use @alwaysNotify instead of @observable.
     @alwaysNotify 
     ObservableMap<String, ParticipantExtended> sortedParticipants = ObservableMap<String, ParticipantExtended>();
  2. override == and hashCode in ParticipantExtended. Avoid unnecessary observable notifications of Iterable or Map fields.

@amondnet amondnet added the question Further information is requested label Dec 14, 2023
@alxmoroz
Copy link

Same for ObservableList etc.

How can I notify observer now, when changing attrs on objects within list or map? Equality checks only addresses in memory, but not theirs contents...

Is this tool only suitable for simple lists of strings and numbers? Previously, it was possible to work with objects.

@amondnet
Copy link
Collaborator

amondnet commented Dec 15, 2023

@alexggordon

How can I notify observer now, when changing attrs on objects within list or map? Equality checks only addresses in memory, but not theirs contents...

Can you please provide reproducible code? Then I can tell you how to do it. Alternatively, see the documentation.

Could you try one of the following?

  1. use @alwaysNotify.
  2. override == and hashCode in Object
  3. use immutable data class

#851

@amondnet
Copy link
Collaborator

#970

@amondnet amondnet added the investigation Needs some research and investigation label Dec 15, 2023
@alxmoroz
Copy link

alxmoroz commented Dec 15, 2023

@amondnet

Could you try one of the following?

  1. use @alwaysNotify.

I tried it. Without changes. Perhaps I didn’t fully understand how to apply it correctly.

  1. override == and hashCode in Object
  2. use immutable data class

Looks like a suitable solution. I'll try this approach.
Thank you very much!

@amondnet
Copy link
Collaborator

amondnet commented Dec 19, 2023

#971

Make the change in 2.2.3 optional. If you want the use this behavior , modify @observable to @MakeObservable(useDeepEquality: true).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigation Needs some research and investigation question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants