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

Need a way to control listeners when realm.create #5864

Open
somasekharkakarla opened this issue Jun 2, 2023 · 7 comments
Open

Need a way to control listeners when realm.create #5864

somasekharkakarla opened this issue Jun 2, 2023 · 7 comments

Comments

@somasekharkakarla
Copy link

somasekharkakarla commented Jun 2, 2023

Problem

Standard way:
In this example we consider a case of Mobile and Server

For example, In Mobile there is collection called Tasks
Tasks has following fields

  • uid
  • name
  • status
  • priorities

i added a change listener to this object.
whenever i change these fields from my mobile, change listener tiggers and object or list i got from it will be sent to the server.
Perfect till now.

Problem:
when i get response. Here there are 2 scenarios

1. Nothing changed at server side
eg: i changed status to 1.

Req:

  • uid :123232
  • name: test
  • status : 1
  • priorities : 0

Res:

  • uid :123232
  • name: test
  • status : 1
  • priorities : 0

so i take this response and update in realm. Because both Objects are same, change listener will not trigger

2. something changed at server side
eg: i changed status to 1. Before i sent the request, someone changed priorities to 1 in the server.

Req:

  • uid :123232
  • name: test
  • status : 1 //from me
  • priorities : 0

Res:

  • uid :123232
  • name: test
  • status : 1 // server updated response with what i sent
  • priorities : 1 //sent fresh update which it has at server

so i take this response and update in realm using realm.create. Because both Objects are not same. change listener will trigger and says priorities changed

As of now point of truth to send response to the server is the change listener.

In this second scenario, it will keep on calling the server till both mobile side and server side copies are same.

In some scenarios mobile side and server side copies must not be same where i can save the number of calls.

Solution

To stop this. we need to have control on listeners when we use realm.create, like we have Modified.update, we should also have listen true/false

Alternatives

No response

How important is this improvement for you?

Dealbreaker

Feature would mainly be used with

Local Database only

@somasekharkakarla
Copy link
Author

somasekharkakarla commented Jun 5, 2023

It will be also great if we add a way to set debounce for listener.
Example :
i set a status in 1st second
i set a description in2nd second

i present way it will listener trigger twice.
if we a debounce 2 sec here. Listener will be triggered once with props change [status, description]

@takameyer
Copy link
Contributor

takameyer commented Jun 6, 2023

@somasekharkakarla Can you write a code sample on what you would expect for the syntax of this feature? Would this be an extension on our current event listeners?

It is currently possible to add a listener to a collection. The event listener callback lets you see what objects were recently created. Ref: https://www.mongodb.com/docs/realm-sdks/js/latest/Realm.Collection.html#addListener

@somasekharkakarla
Copy link
Author

somasekharkakarla commented Jun 6, 2023

Hi @takameyer

Yes, an extension on our current event listeners.

Syntax example:

Create/Update:
realm.create('Dog', {name: dogName, age: 1}, 'modified');
realm.create('Dog', {name: dogName, age: 1}, {mode:'modified', triggerListener:true/false, debounceForListener: 500}); // if dev want to control listener

Delete:
realm.delete(dog);
realm.delete(dog,{triggerListener:true/false, debounceForListener: 500}); // if dev want to control listener

Syntax should both ways

@takameyer
Copy link
Contributor

If we were to do this, it would have to be done at the addListener function and not at the realm.create. This would also need to be done at the core level for this to be effective.

We will have to discuss internally if this is something we want to set aside resources for. It does make sense to be able to refine the event listeners so they aren't as noisy.

What we could add, which is actually in core are "key path listeners". These would enable the user to provide which fields they are interested in and only fire the listener when they change.

Key Path Listeners in swift

@somasekharkakarla
Copy link
Author

somasekharkakarla commented Aug 4, 2023

if we done at addListener there is a problem.

Suppose i have a model called model 1

Adding listener to model 1 = > model. addListener (which will listen for an update)

now i am add two objects to model1 in two situation like

realm.create("model1", {uid:'1', name:'test1'}) => i want to listen this
realm.create("model1", {uid:'2', name:'test2'}) => i don't want to listen this

if we do it at addListener level how we achieve it

@somasekharkakarla
Copy link
Author

hi @takameyer,
Any update on this?

@takameyer
Copy link
Contributor

takameyer commented Feb 3, 2024

@somasekharkakarla It is in our backlog, but we haven't gotten to it yet. Although we have recently released support for key path listeners. With this it would be possible to set which fields will fire the event listener. In the case above it could be used as a workaround by creating an optional field to listen to in the model.

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

No branches or pull requests

2 participants