I am trying to display the sleep from the HealthKit. I am using AppCore
to display other HKQuantity. I use the following for HKQuantities like Steps, etc.
[[APCScoring alloc] initWithHealthKitQuantityType:HKQuantityType
unit:[HKUnit countUnit]
numberOfDays:-kNumberOfDaysToDisplay];
My issue is that sleep data is not a HealthKitQuantityType and I can't use HKStatisticsCollectionQuery.
I am looking to display HKCategoryValueSleepAnalysisAsleep.
As you probably figured out, Healthkit sleep analysis is not quantitative.
As describe in the Apple documentation you have only 3 states: inBed, aSleep or awake.
I've got same question and to work around, I count minutes aSleep versus minutes inBed and minutes awake (depending of what's relevant to you) based on startDate and endDate. Then I display the result in an histogram chart or similar.
If you're looking for a way to fetch or save sleep analysis data with Healthkit, I've written a post a year ago here that can eventually help you.
Related
I'm need to query HealthKit for HKCategoryTypeIdentifierSleepAnalysis data, but can't find the compatible HKUnit for quantity value. Apple documentation is silent on units for Sleep Analysis. Am hoping someone already knows the answer.
BTW, the iOS Health app shows Hrs & Minutes on the Sleep chart, but the HKUnit reference doesn't include options for such composite units.
In Apples documentation I found this:
By comparing the start and end times of these samples, apps can calculate a number of secondary statistics: the amount of time it took for the user to fall asleep, the percentage of time in bed that the user actually spent sleeping, the number of times the user woke while in bed, and the total amount of time spent both in bed and asleep.
This means that you have to use the startDate and endDate property of your sample to calculate sleep durations.
Sleep samples are instances of HKCategorySample, which is unit-less. You should perform calculations for sleep samples using the startDate and endDate properties on the sample.
SO basically all is in the title. I've searched quite a lot, but didn't find any right solution which doesn't require internet connection.
If the user changes time in settings - i can't find real time since last launch.
I need that for my game, in it for every hour, even when you don't play the game, you get some coins.
If the user changes time in settings - that affect the time in NSDate() and user can cheat with coins.
So save the NSDate() to user defaults on app launch. The next time the app comes to the foreground, or gets launched again, get the current NSDate and subtract the saved date from it. That will give you the number of seconds between the two dates. Calculating hours from seconds is a simple matter of dividing by 3600. – Duncan C just now edit
EDIT:
Note that in newer versions of Swift (starting with Swift 2?) Most Foundation classes were defined as native Swift classes without the NS prefix. For newer versions of swift, replace all occurrences of NSDate with Date in the above.
Also note that in iOS ≥ 7.0, the Calendar has some methods that make this sort of calculation neater and easier. There's a new method dateComponents(_:from:to:) that lets you calculate the difference between 2 dates in whatever units you want. You could use that to calculate the seconds between the 2 dates more cleanly than calculating seconds, as outlined in my original answer. Calendar methods also tend to handle boundary conditions like spanning daylight savings time, leap seconds, etc.
Consider the following Swift 4/5 playground code:
import UIKit
let now = Date()
let randomSeconds = Double.random(in: 100000...3000000)
let later = now + randomSeconds
if let difference = Calendar.current.dateComponents([.second],
from: now,
to: later)
.second {
print(difference)
Try this.
Step 1. When user exits game. Set a NSUserDefault with current time.
Step 2. When app launches, in your appDelagate file, get this value.
Step 3. Calculate diff between and award coins accordingly.
I am trying to monitor a post, and plot the number of ups and downs over a 24 hour period (at 5 minute intervals). The core of the code looks like this:
while True:
post = r.get_submission(submission_id='23a1zz')
time.sleep(5)
post.refresh()
print post.ups
time.sleep(5*60)
However, it does not reflect the true ups and downs. It's stuck at the same number even though the actual post is pretty dynamic.
The API Guidelines state that the same resource shouldn't be requested more often than every 30 seconds. This guideline is backed by a cache, on both Reddit and PRAW's end that will return the same content if requested again within a short while. http://praw.readthedocs.org/en/latest/pages/faq.html#i-made-a-change-but-it-doesn-t-seem-to-have-an-effect
I want to have a countdown from the current time to a specific schedule like this:
8:48, 9:29, 11:56, 12:36, 13:18, and ect. and display that value in a label.
For example. Now we have 11:00 o'clock. And UILabel tell us 56 minutes remaining. And after 11:56 it tell us 40 minutes remaining (from current 11:56 to 12:36).
I don't ask you code directly, just help to find right way to search.
Thanks.
some links by searching google
Implementing a Countdown Timer in Objective-c?
http://looksok.wordpress.com/2012/12/29/ios-timer-countdown-implementation-tutorial-including-source-code/
and implementation
https://github.com/jonhernandez/iOS-Timer-Countdown
I havent looked at code quality, but it should get you started
a relatively simple question that I've not been able to find a clear answer to. My app is more complex, but answering this question will suffice.
Suppose you're writing a stopwatch app. When the user taps "start", the app stores the current date and time in startTime:
startTime = [NSDate date];
When the user tapes "stop", the app stores the current date and time in stopTime:
stopTime = [NSDate date];
The duration is calculated by:
duration = [stopTime timeIntervalSinceDate:startTime];
and is displayed with something like:
[durationLabel setText:[NSString stringWithFormat:#"%1.2f", duration]];
The typical durations that my app is timing range from 2 to 50 seconds. I need accuracy to 1/100th of a second (e.g. 2.86 seconds).
I'm assuming that there is some protocol that iOS devices use to keep their clocks accurate (cellular or NTP sources?). My concern is that between starting and stopping the stopwatch, the clock on the iOS device is updated which can result in a shift of the current date/time either ahead or back. If this were to happen, the duration calculated would be inaccurate.
I've seen a few posts relating to timing methods for purposes of improving code efficiency. Some suggest using mach_time.h functions, which I'm not familiar with. It's not obvious to me which is the best approach to use.
Is it possible to disable iOS from updating the date & time? Is mach_absolute_time() unaffected by iOS clock updates?
Many thanks!
Tim
You are correct in thinking that CFAbsoluteTime and its derivatives (NSDate dateand so on) are potentially skewed by network updates on 'real' time. Add that to the fact that NSTimer has an accuracy of 50-100ms and you have a timer that is not suited to the most critical of time-sensitive operations.
The answer to this problem seems to be CACurrentMediaTime.
It is a member of the Core Animation group, but there shouldn't be any problem integrating it into non-animation based applications.
CACurrentMediaTime is a wrapper of mach_absolute_time() and makes sense of the "mach absolute time unit," which from my understanding is no fun to tinker with. mach_absolute_time() is calculated by running a non-network synced timer since the device was last booted.
There is relatively little information on CACurrentMediaTime but here are some sources and further reading:
Apple's sparse documentation of CACurrentMediaTime
Stack Overflow - NSTimer vs CACurrentMediaTime()
http://bendodsonapps.com/weblog/2013/01/29/ca-current-media-time/
http://blog.spacemanlabs.com/2011/09/all-in-the-timing-keeping-track-of-time-passed-on-ios/
http://forum.sparrow-framework.org/topic/accurate-timer
Note: If you do use CACurrentMediaTime, make sure you include and link the QuartzCore.framework
Check out this here. I would say forget about the current time check and use a precision timer since it won't rely on the current time but instead uses an interval.