Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can an iOS app access the time of events using the iOS clock?
This would seem to be a simple question… but couldn’t find anything on Google search or here on Stackoverflow either.
Q1. I’m doing a feasibility study on building an app that needs to record the precise moment an event occurs. For example, if an event in astronomy, nature (e.g. lightning) occurs at 16h18m22s (4:18pm, 22 seconds), and the app supports the user to record when the event occurs, can the exact time the user taps the button be recorded using the iOS clock as a time reference?
Q2. How much precision is offered by the iOS clock? Is it possible to record the event-time as a decimal of one second? Milliseconds?
For example: 16h18m22s50c (where the last two digits represent 50 centiseconds or 0.5 of a second).
Q3. Would it be safe to assume that, apart from timezone differences, that everyone’s iOS device is reading exactly the same time, what one might call "universal device time"?
Q4. Or if this approach using the iOS clock seems a bit clunky, especially where ultra accuracy is required, would it be smarter to get a feed from an atomic clock server?
Thanks for any input on this one. Appreciated!
Cheers
Q1: yes! Using [[NSDate date] timeIntervalSince1970] you get sub-millisecond precision
Q2: you are going to get milliseconds as explained in Q1.
Q3: Since timeIntervalSince1970 gives you exactly what it says (the time interval since 1970), yes - that is a property unaffected of timezones. (watch out for the but below)
Q4: using a server will destroy any way of retrieving an accurate result since you have to contact the server and wait for a response, that will take around 100ms which is far worse than every inaccuracy the device clock would have.
BUT
the user can change its device time which makes the measurement still exact but useless since it no longer reflect the actual time! What you can do in this case is
retrieve the current timestamp of the action on the device
contact a server with a properly set up clock
compare the measurement and ignore the measurement if it is too far off / alert the user to correct his system time. A comparison threshold of half a second should be okay unless your server is very far away or very slow.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm learning how to parse JSON from web APIs. I have read that I need to asynchronously return my parsed data from the API for my application, as opposed to synchronously. I'm not sure why it has to be asynchronously. I know it has something to do with threading, but that doesn't clarify it for me.
Why do network requests have to be performed asynchronously?
The fact that you should to do this asynchonously has nothing to do with the nature of the response (JSON or otherwise). It’s just that you’re requesting data from an API on a remote server and you don’t know how long it will take (subject to the nature of the network the device is on, how busy the web server is, etc.).
Bottom line, any task that takes more than a few milliseconds should generally be performed asynchronously to ensure a responsive UI, and this API call will take much more time than that.
Analogy time
Imagine that you're employed in the information booth of a train station to manually update a board with trains' statuses. You read off an old-fashioned ticker tape and move models of the trains around so that passengers can see what's going on. You also answer questions about schedules and such directly, when passengers ask you.
You realize that for one particular portion of the board, some information is missing from your tape. Your colleague has the info, but she isn't in the station. So you leave the board, go over to the phone, and call her. You dial, and wait for her to pick up, and then explain what you need. She doesn't have what you need immediately to hand, so she asks you to wait a moment while she gets it.
Meanwhile, the tape doesn't stop. Information about trains continues to come in. but because you're sitting there on the phone waiting, you're not doing anything with it. The people who are watching the board get frustrated, and the people who have questions for you can't ask them either.
Finally, your colleage comes back and gives you what you asked for. You thank her and return to the board. You realize the board is in very bad shape, not reflecting the current state of the world at all. And the passengers with questions have stormed out and left you a one-star review on the App, I mean Train, Store.
Next day, the same situation comes up. You need information you don't have. Instead of stepping away from the board for several minutes, you quickly fire off a text message, and get right back to talking to passengers and moving things around on the board.
In about the same amount of time that you spent waiting on the phone yesterday, you get a text back from your colleague with the information. You incorporate it into your workflow, and nobody even notices that you spend a couple of seconds reading from your phone instead of the ticker tape. Victory!
The first day, you made a synchronous network request. You initiated a conversation with a remote service and waited until you got the response. When that happened, your UI was locked up, neither taking input from the user nor refreshing its own state.
The second day, you made an asynchronous request. You kept working normally until the response came back, allowing you to continue with all your other tasks.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
That application I am making has 5 days trial period. Please tell me what is the best way to show alert or something after trial period is over. I want to use the method which drains less battery and is more efficient. Following are two ideas.
Timer:
Let fiveDays = 5 * 24 * 60 * 60
Timer.scheduledTimer(withTimeInterval: fiveDays, repeats: false, block: { (timer) in print("Hi!")})
DispatchQueue
Let fiveDays = 5 * 24 * 60 * 60
DispatchQueue.main.asyncAfter(deadline: .now() + fiveDays, execute: {
self.functionToCall()
})
Please suggest:
What is the best approach? Is it Timer or DispatchQueue
Why is it the best approach?
It would be a lot better to do this on the server-side. Simply register the device id onto the server and then, when your app start check whether its been 5 days since that device has registered. This would be the best approach to accomplish what you want to do.
Neither one would work without on-device persistence, and neither is a good idea even with persistence.
The best way would be to save an initial start date to the device the first time the user logs in, and then check that date each subsequent log in to see if 5 days has passed yet.
Both the approaches you wish to use are not much useful.
You can do this with 2 approaches.
1) Store all the user information in your server but in order to do that you may need to register your user first and then on the app side you may disable some feature or whole app depending your requirements when trial period expires.
2) If you dont want to do it using your backend server then you can store device unique id in device keychain and later retrieve it if user uninstalls and then installs again, this way you can find whether use deleted the app earlier or not and check your trial period.
Thanks
Set the date in UserDefaults at first launch.
After every launch compare the dates.
And my advice to you is that do the check on server side.
Following are the options to track time period of application. But both of these ways can easily be demolished by re-installing/changing device time.
Keychain (persists even if user re-install application)
UserDefaults (reset on re-install of application)
Solution: In order to overcome above mentioned issues, we can track trial time period by keeping record in Keychain and taking time online reference, this requires us to make an API call(There are number of such online services that provides you with exact server time with API hit).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I was wondering if anyone can explain the flow of an app using core location. I am building a GPS app that will pause (after a given delay set by the user) and then run through the iterations (also set by the user). But I am finding that it runs indefinitely. If I take the for loop out of the code I then get 2 responses back from location manager on my first search.
Effectively this is: user presses the "get location" button. This then calls our getlocation function but it loops twice through this and through the NSLog output I will always see 2 responses but only on the first loop.
Getting location updates is an asynchronous project and you need to wait for, then filter the results. The flow is:
Create a location manager and set your object as it's delegate.
Ask it to start updating your location
Return.
Then you will start receiving location updates. The first one will probably be "stale" (the timestamp on it will be from the last time the GPS was active.) Make your code reject any location update who's timestamp is more than 1/2 second greater than now to solve this problem.
Next, you will receive a series of location updates with really bad (unacceptably bad) horizontal accuracy readings. Check the accuracy reading and reject all readings that are not accurate enough for your purposes. (The accuracy reading is actually a radius value that defines a circle where the location might be, expressed in meters. Smaller is better.)
How accurate is accurate enough depends on your needs. If you can't get an accurate enough reading in a reasonable time (say 30 seconds) make sure you code a timeout that gives up and displays a "can't get an accurate reading" message to the user.
Once you either get a recent reading that's accurate enough, or time out, stop updating until the next time you need a location fix (or keep updating if your app requires that.)
You might want to write a test app that displays timestamps, map positions, accuracy readings, etc as they come in, and then walk around with your phone while it's running. It takes a fair amount of tinkering to get an algorithm that filters location updates the way you need it to.
This question already has an answer here:
iOS Detecting connection speed or type [duplicate]
(1 answer)
Closed 9 years ago.
Using Objective C, I need find the specific bandwidth of the network an iOS is connected to. Does anyone have any hints for finding this information with code?
You just need to place a file big enough in one of your servers (or somewhere else)
This could be done using a normal NSURLConnection.
Before start downloading, set up a timer. When your file's been downloaded, you stop the timer. Then you have the amount of time it took for the device to download X MB. You can do the calculations based on this. Once this has been done, the file can be discarded.
Of course, this will vary depending on how far the device is from the server, ping, routing, etc... but I think it's good enough for an implementation this simple. If you want more accuracy repeat the operation several times and calculate average speed.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a clock in iOS that can be used that cannot be changed by the user
I know in many iOS games that have tasks that take time (i.e. "10 hours until animal done breeding"), one can go into "Settings" and change the time to speed up the completion of the task. In the game I'm developing, I don't want users to do this, so I'm trying to implement a system to prevent this type of cheating. I've put a lot of thought into this, but can't come up with a solution. Basically, my question is how do I keep track of time without relying on the system clock (which can easily be manipulated) for the purpose of preventing users from cheating?
transforming my comment into an answer.
Instead of relying on a user or device, rely on an external source for providing unbiased time. A time server, your server, etc. If you don't trust....
Instead of "X hours of real-world time", would "Y minutes of in-app time" work for you? You can pause/continue a timer when your app suspends/resumes.