I have a question about managing timers in Swift.
Currently I have a ViewController that takes care of calling a rest network service, every 30 seconds, through a Timer that I initialize inside ViewDidLoad with an and with the value repeats: true.
Up to now there are no problems, the functioning seems quite correct to me.
However, I would like to understand a timer feature.
Let's say I decide to decrease the time from 30 seconds to 1 second, in this case, what happens with the network requests? From what I understand the timer, being scheduled every 1 second, this makes 1 request every second, but as we all know, there could be a delay in the response from the backend.
In this case, are subsequent requests queued?
Is there any way to say, wait for the backend response and then execute the second request?
Related
How to test implemented fixed delay on webpage with Playwright (exactly: debounce)?
I have simple scenario. User, after entering input need to wait fixed time for response i.e. 1000ms.
How to test that exact wait with Plawright?
Looked at https://github.com/microsoft/playwright/issues/4405 but I wonder if there is more elegant way to do this?
Well usually there is a better way than to hardcode a waiting time.
Wait for certain API calls
Wait for network idle (= wait for started calls to finish)
Wait for an event
However there are times when hardcoding is inevitable. What I've used is page.waitForTimeout()
Wait for 2 seconds:
await page.waitForTimeout(2000);
Official docs: https://playwright.dev/docs/api/class-page#page-wait-for-timeout
"Waits for the given timeout in milliseconds.
Note that page.waitForTimeout() should only be used for debugging.
Tests using the timer in production are going to be flaky. Use signals
such as network events, selectors becoming visible and others
instead."
I have a AFHTTPRequestOperationManager subclass that I use for API calls to an external service. They only allow 30 API calls per 60 seconds, so I need to make sure I keep track of how many I have used.
Is there a way to know each time I have made a request with the operationManager and then keep a timer going so I know how many requests have been made? Then reset the count after 60 seconds? Or should I just save an NSDate object and then check to see if it has been 60 seconds?
I need to send around 20 HTTP Post requests in my iOS Application. Right now I am using NSURLConnection and sending the 20 requests one by one, which of course takes a long time. Each connection starts after the previous, taken around 7 seconds to complete all the requests. Is it possible to send these 20 requests simultaneously and receive the JSON result much faster?
You can use NSOperation and NSOperationQueue to prepare all of the requests and push them onto the queue at the same time. Then you can set the concurrent execution limit to determine how many run at the same time. Don't run all 20 at the same time though as you may flood the network and prevent any of the connections from completing properly. Try running 5 concurrently and see how it goes.
I have a requirement where I need to know what is the server time at a given point of time in the app. As soon as the app connects to server, the server sends back the time and I am not sure how to update this time.
I thought of using the timers where the method is called every second and and a second is added to server time, so that whenever I ask for server time it is always updated one. But problem with this if we schedule this on main runloop, the run loop may or may not process the timer request if there it is busy.
So how to track the server time?
Thanks
Your server is in a specific time zone correct? Just get the time zone of the server using a request and show the time in that time zone in your app. You can then use a timer to continue updating the time every second.
I suggest getting just the time zone because if you get the server's time, there would be a lag between when the server sends back the response and when you get it - which defeats the whole purpose of getting the time as on the server.
You can't update the iOS system time from your app; there is no API
for that.
iPhone's time is usually very accurate. On iPads, it varies but has improved with iOS 5 to +/- 5 seconds if some form of internet connectivity is available.
If you want to manually connect to a server to synchronize time, you
should do it in a background task.
To compensate for latency, you should make multiple requests and add
the average half of the roundtrip time to the time sent by the server.
However, the question remains: Why do you want to do that anyway?
i want implement a windows services scheduled task.
I already created windows service. In a service i have implemented a timer.The timer is initialized at class interval. The timers interval is set in the start method of service and also it is enabled in the start method of the service. After timers elapsed event is fire i have done some actions.
My problem is that, i am in a dilemma. Lets say the action i have done in Elapsed event, lets say take one hour and the timers interval is set to half an hour. so there are chances that even if the previous call to elapsed event has not ended new call to elapsed event will occur.
my question will there be any conflict or is it ok or shall i use threads.
please give some advice
You can stop the timer at the begining of your long running method, then start it again at the end:
Timer.Change(Timeout.Infinite, Timeout.Infinite)
// do long task ...
Timer.Change(dueTime,period)