CombineCommunity / CombineCocoa

Combine publisher bridges for UIKit
MIT License
1.65k stars 132 forks source link

UITextField textPublisher emitting twice #69

Closed vinczebalazs closed 2 years ago

vinczebalazs commented 2 years ago

Describe the bug I have come across a strange behaviour, where the textPublisher will emit the same value twice, if there is a subscription to it, and at the end of the subscription when the keyboard is dismissed (if it has not been dismissed before the subscription occurred).

To Reproduce Imagine a save method that starts its chain by subscribing to the textPublisher, sending the value to an API, and on completion popping the view controller that contained the text field. The chain in the save method will be triggered twice, once when the method is invoked, and once when the view controller is popped, as that will cause the text field to dismiss the keyboard and emit an event (not sure which exactly) stating that editing has ended on it. I believe that this is due to the fact that the textPublisher observers both .allEditingEvents and .valueChanged events.

Expected behavior I would expect the textPublisher to only emit when the actual text has changed, not when editing has ended or anything similar.

Device:

ahmedgomaa27 commented 2 years ago

textPublisher emit for all editing events, so it'll emit when you click on the text field, resign responder from and then edit the text, also if you see the implementation, you'll find that in request(_ demand: Demand) function, subscriber call receive method to send the initial value so that it'll emit for the text publisher when you just subscribe on it

vinczebalazs commented 2 years ago

@ahmedgomaa27 Yeah I understand why it's happening, it's just that I would expect a textPublisher to emit when the text changes, should not be releated to other events. Maybe break this up into two different publishers? @freak4pc

ahmedgomaa27 commented 2 years ago

@vinczebalazs you can use controlEventPublisher and get the value from the text field directly , in this case you send the required event for u

freak4pc commented 2 years ago

Hey @vinczebalazs - What @ahmedgomaa27 mentioned is correct. The default is tracking all editing events for a text field (copied behavior from RxCocoa here). If you want a specific control event, you can use controlEventPublisher directly, as mentioned above.