iOS RXSwift What is the equivalent of debounce which lets through first event only? - swift4

I have a button which sends network requests. I want to ignore double taps within this button. I tried .throttle, but that didn't seem to work. So I'm trying .debounce - this indeed ignores duplicate taps, but only lets through the last event.
What I want is: On first tap, send a network request, ignore subsequent taps within 0.33 seconds.
Which RXSwift operator would help me put an event through and disable subsequent events within a time window?
let buttonPressObservable = button.rx.tap.asObservable()
buttonPressObservable.debounce(0.33, scheduler: MainScheduler.instance)
.map{/*do stuff*/}

In version 4.2 of the library, throttle has a parameter latest that is set to true by default. If you set that to false, I think it will give you the behavior you want.

Related

How to I resolve GTM Trigger not firing as expected

I would like to set a tag to only fire on my homepage so have set a trigger
url path equals /
When I preview or debug the tag it seems to be firing on all pages of my site, when I then review the firing triggers it tells me its fired because of a filer (_event equals gtm.js). I have no idea where this filter comes from or how its been implemented as it isnt in the trigger I set up. Any ideas how I remove this so the tag doesn't always fire?
You're misreading the debugger. Your first screenshot here:
indicates that the the trigger did not fire, as is indicated by the top X.
For a trigger to fire, all conditions (filters) have to pass. If even one condition doesn't pass, the trigger won't fire. Well, there are also blockers, but we'll leave them out for now.

How to make a button trigger an event (Timer)?

I am working on a fire evacuation project and would like to trigger an event after clicking a button. Basically I want to begin the fire evacuation process manually using the button feature but would like to trigger a timer for about 60 seconds to give the pedestrians time to evacuate using the event feature.
I have tried to make pedestrians stop the evacuation process by manually clicking another button but would like to use an event trigger timer instead as this will be a better way to conduct my simulation. I cannot seem to trigger an event once the button is clicked. I've tried changing the trigger type but not sure where to go from there. Is there any code that is required or another step that is required?
Even though a dynamic event is fine in your application, you can also use a user control event:
And then in your button you can use the following code:
event.restart(60,SECOND); //you can choose any timeout here
or
event.restart(); // this will use the configuration, which in this case I set up to 60 seconds
The difference between a dynamic event and a user controlled event is that if you click the button again:
With the dynamic event, you will generate another instance of the event, meaning that if you click the button at t=0 seconds, and t=30 seconds, you will generate 2 events at t=60 seconds and t=90 seconds
With the user control event, you will restart the same event. if you click the button at t=0 seconds, and t=30 seconds, you will generate a unique event at t=90 seconds.
So depending on which one you prefer... you can choose. I would prefer the user control in case you click the button 2 times... or if you regret clicking it so fast... I don't know
Use the Dynamic Event for this.
Set it up by pulling it from the AnyLogic Agent Library to the Workspace and give it a name (here: MyDynamicEvent).
Add the code you want to have executed when the timer runs off in the Action field
Set one (or several) timed instance for this event by using the code create_MyDynamicEvent(60,SECOND);. Note: The syntax of this statement is always create_ followed by the actual name you gave your DynamicEvent type.

swift - how to prevent two touches at the same time

For example, I have 2 buttons Change email and Change password, and each of them call functions with Alamofire request, and responce data should reload both the UI and data scheme.
The point is that this PUT requests change not only servers's data, but generate new token and get updated user's profile.
And when pressing buttons at the same time, at the same moment touches begin and end, app crash after parsing requests.
I'm blocking another UI elements(like textfields), I was trying to block another button, but when press it together, it's not works.
So how can I prevent the same time touch? I'm not good at OperationQueue, maybe thats'the way? Is there an option to check if operation not first at the queue and kill it?
Set isExclusiveTouch of your UIButton to true in order to trigger only one button action in a specific time.
This code will get all the buttons contained in the view and set the exclusiveTouch to true:
self.view.subviewsRecursive()
.filter { $0 is UIButton }
.forEach { $0.isExclusiveTouch = true }
This problem with the UIResponder object is very usual. However, your problem description is not clear and your implementation seems not so good.
Here, to resolve this quick touch event problem:
Your solution is debouncing the action event of UIButton.
Debouncing also helps to prevent multiple executions when a user mistakenly pressed a button (or any UIResponder object) multiple times so quickly that even the UI was not blocked till then. Following article may guide you more regarding the same:
Debouncing to tackle repeating user action

How to mark the NSEvent as handled?

I handle pressing of letter keys on keyboard with the following code.
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown) { event in
// code
}
How do I then mark the NSEvent as handled, not allowing it to be handled again by system and another apps? For example do not allow the input of a pressed letter in the opened text editor?
You can't do that with NSEvent. As the docs note:
Events are delivered asynchronously to your app and you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application.
You're not part of the event-generation system; you're just getting notifications as part of your runloop.
If you want to become part of the event system, below the app layer, you need to use CGEvent. See tapCreate(tap:place:options:eventsOfInterest:callback:userInfo:). The callback can return NULL to indicate it's consumed the event.

Can I control how long Chrome App notifications last?

We've got a Chrome app with working notifications, but the notification only lasts a few seconds and then goes away. Our end users are going to want that notification to stay until they dismiss it. Can that be done? We couldn't find anything in the API documentation regarding notification duration.
Update
Since Chrome 50, you can make a notification persist until dismissed with requireInteraction flag:
Indicates that the notification should remain visible on screen until the user activates or dismisses the notification. This defaults to false.
Old answer
You can't make a chrome.notifications notification last forever.
You can sort of control the duration with the priority parameter. From the relevant article (not the main documentation):
Notifications can be assigned a priority between -2 to 2. Priorities < 0 are only shown in the center; priorities > 0 are shown for increasing duration and more high priority notifications can be displayed in the system tray.
Note: if you call update changing the notification's priority, it will be re-shown as needed. You can, in principle, try to exploit that to stay visible.
If you do not need features provided by Rich Notifications, you can use web standard Notification API.
They look similar in Chrome, but do not integrate with the Message Center. As such, they do not disappear unless dismissed by user or programmatically.