I am syncing Heart Rate and BloodPressure data from HealthKit.
The problem with this approach is when the user enters historic data which will not be synced. How do I perform the same query but with CreationDate (instead of StartDate), or some kind of database ID which will identify the historic value as being newer?
I just want to filter out all the newly created values from healthkit.
-(void)getSpecificHealthKitDataHeartReat:(NSDate*)myDate
{
NSDateFormatter *dtFormat = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
NSDate *startDate = [calendar dateFromComponents:components];
NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];
//[HKQuery predicateForObjectWithUUID:(nonnull NSUUID *)]
//Read HeartRate
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKQuantityType *heartRateType2 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKSampleQuery *sampleQuery2 = [[HKSampleQuery alloc] initWithSampleType:heartRateType2 predicate:predicate limit:0 sortDescriptors:#[timeSortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
{
if (!results)
{
NSLog(#"There are no heart rate results. The error was: %#.", error);
return;
}
else
{
NSMutableArray *hrArray = [[NSMutableArray alloc]init];
for(HKQuantitySample *samples in results)
{
HKQuantity *hrQuantity = [samples quantity];
// double hr = [hrQuantity doubleValueForUnit:[HKUnit unitFromString:#"count/min"]];
double hr = [hrQuantity doubleValueForUnit: [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]]];
NSLog(#"hr %f",hr);
NSLog(#"startDate %#",samples.startDate);
NSLog(#"endDate %#",samples.endDate);
}
}
}];
// Execute the query
[healthStore executeQuery:sampleQuery2];
}
Use HKAnchoredObjectQuery (documentation here). It is designed for exactly this use case.
I am new to parse and I need help retrieve objects that are in the current month.
First I have a class called food and have a column of type Date called food_date
I am assuming I have to use greaterThan and less Than, but I am unsure how to.
NSDate *thisMonth = [NSDate date];
PFQuery *foodList = [PFQuery queryWithClassName:#"food"];
[foodList whereKey:#"food_date"
I think you need something like this :
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
components.day = 1;
// Get the date of the first day of the current month
NSDate *minimumDate = [calendar dateFromComponents:components];
components.month = components.month + 1;
// Get the date of the first day of the next month
NSDate *maximumDate = [calendar dateFromComponents:components];
PFQuery *foodList = [PFQuery queryWithClassName:#"food"];
[foodList whereKey:#"food_date" greaterThanOrEqualTo:minimumDate];
[foodList whereKey:#"food_date" lessThan:maximumDate];
[foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(#"Successfully retrieved %d foods.", objects.count);
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(#"%#", object.objectId);
}
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
I hope that help.
Here's the full description of my problem: I am fetching the events of a calendar for a full day (i.e.today) and storing them in an array. How do I isolate the next relevant (one that has not passed) event's title and time from the array to display them separately as labels?
Here's my code:
//Load Calendar Events
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,
NSError *error) {
if (granted) {
NSLog(#"User has granted permission");
// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Create the start date components
NSDateComponents *beginDayComponents = [[NSDateComponents alloc] init];
beginDayComponents.day = 0;
NSDate *todayStart = [calendar dateByAddingComponents:beginDayComponents
toDate:[NSDate date]
options:0];
// Create the end date components
NSDateComponents *endDayComponents = [[NSDateComponents alloc] init];
endDayComponents.day = 0;
NSDate *todayEnd = [calendar dateByAddingComponents:endDayComponents
toDate:[NSDate date]
options:0];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:todayStart
endDate:todayEnd
calendars:nil];
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];
NSLog(#"Here are the events in the array, %#", events);
} else {
NSLog(#"User has not granted permission");
}
}];
Thanks in advance, and have a good day!
As apple states in its EKEventStore-documentation you will have to sort your array first, so that the next pending event is at index 0.
Note: Retrieving events from the Calendar database does not
necessarily return events in chronological order. To sort an array of
EKEvent objects by date, call sortedArrayUsingSelector: on the array,
providing the selector for the compareStartDateWithEvent: method.
I'd suggest you then just pick the EKEvent-Object at index 0 of your array and read the properties from it and set them on your label.
EKEvent *event = [events objectAtIndex:0];
yourTitleLabel.text = event.text;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateformat = #"dd.MM HH:mm";
yourDateLabel.text = [formatter stringFromDate:event.startDate];
Edit:
You would sort your array events like this:
events = [events sortedArrayUsingSelector:#selector(compareStartDateWithEvent:)];
For this to work, you have to import EventKit/EventKit.h
I am making a reminder app, where user create some event with date and time and choose when he should get noticed about it by choosing a fire date (interval before this event: right now, 5, 15, 30 minutes ago, and so on) and repeat interval (never, daily, weekly, monthly and every year). The problem is: when user creates an event, that already happens, for example event should occur in 10 april, and today is 16 april, he should be reminded about this event right in time, but user gets notification about this event right after creating it. So it shouldn't happened. How can I avoid this?
Here is the method, that creates notification
- (void)notificationWithNote:(Note *)scheduledNote deleteThisNotification:(BOOL)deleteNotification {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:scheduledNote.date];
ToDoItem *todoitem = [[ToDoItem alloc] init];
todoitem.day = [comp day];
todoitem.month = [comp month];
todoitem.year = [comp year];
todoitem.hour = [comp hour];
todoitem.minute = [comp minute];
todoitem.eventName = scheduledNote.event;
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:todoitem.day];
[dateComps setMonth:todoitem.month];
[dateComps setYear:todoitem.year];
[dateComps setHour:todoitem.hour];
[dateComps setMinute:todoitem.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
if ([scheduledNote.remindTime intValue] == 1)
localNotif.fireDate = itemDate;
else
localNotif.fireDate = [itemDate dateByAddingTimeInterval:-([scheduledNote.remindTime intValue]*60)];
switch ([scheduledNote.repeatOption intValue]) {
case 0:
localNotif.repeatInterval = 0;
break;
case 1:
localNotif.repeatInterval = NSDayCalendarUnit;
break;
case 2:
localNotif.repeatInterval = NSWeekCalendarUnit;
break;
case 3:
localNotif.repeatInterval = NSMonthCalendarUnit;
break;
case 4:
localNotif.repeatInterval = NSYearCalendarUnit;
break;
default:
break;
}
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%# begins", nil), scheduledNote.event];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:todoitem.eventName,ToDoItemKey, #"Timenote is coming", MessageTitleKey, nil];
localNotif.userInfo = infoDict;
if (deleteNotification)
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
else
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"fire date: %#",[localNotif.fireDate description]);
[todoitem release];
[localNotif release];
}
This bug happens only if user doesn't choose any repeat interval. If event repeats every day/week/month/year, remind appears right in time. So the problem is actual if localNotif.repeatInterval == 0
Looks like, I've found the answer. If issue happens only, when there is no repeat interval, then it needs to check this condition: if fireDate is less then current date and if there is no repeat interval, than this notification shouldn't be scheduled.
NSComparisonResult result = [localNotif.fireDate compare:[NSDate date]];
if (((localNotif.repeatInterval == 0) && (result == NSOrderedAscending)) || deleteNotification)
{
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
else
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I have an issue related to calculating business days in Objective-C.
I need to add X business days to a given NSDate.
For example, if I have a date: Friday 22-Oct-2010, and I add 2 business days, I should get: Tuesday 26-Oct-2010.
Thanks in advance.
There are two parts to this:
Weekends
Holidays
I'm going to pull from two other posts to help me out.
For weekends, I'm going to need to know a given date's day of the week. For that, this post comes in handy:
How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?
For holidays, #vikingosegundo has a pretty great suggestion on this post:
List of all American holidays as NSDates
First, let's deal with the weekends;
I've wrapped up the suggestion in the post I cited above into this nice little helper function which tells us if a date is a weekday:
BOOL isWeekday(NSDate * date)
{
int day = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date] weekday];
const int kSunday = 1;
const int kSaturday = 7;
BOOL isWeekdayResult = day != kSunday && day != kSaturday;
return isWeekdayResult;
}
We'll need a way to increment a date by a given number of days:
NSDate * addDaysToDate(NSDate * date, int days)
{
NSDateComponents * components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate * result = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0];
[components release];
return result;
}
We need a way to skip over weekends:
NSDate * ensureDateIsWeekday(NSDate * date)
{
while (!isWeekday(date))
{
// Add one day to the date:
date = addDaysToDate(date, 1);
}
return date;
}
And we need a way to add an arbitrary number of days to a date:
NSDate * addBusinessDaysToDate(NSDate * start, int daysToAdvance)
{
NSDate * end = start;
for (int i = 0; i < daysToAdvance; i++)
{
// If the current date is a weekend, advance:
end = ensureDateIsWeekday(end);
// And move the date forward by one day:
end = addDaysToDate(end, 1);
}
// Finally, make sure we didn't end on a weekend:
end = ensureDateIsWeekday(end);
return end;
}
Note; There is an obvious optimization I skipped - you could easily add more than one day at a time to the current date - but the point of my post is to show you how to do this yourself - and not necessarily to come up with the best possible solution.
Now lets tie that up and see what we have so far:
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDate * start = [NSDate date];
int daysToAdvance = 10;
NSDate * end = addBusinessDaysToDate(start, daysToAdvance);
NSLog(#"Result: %#", [end descriptionWithCalendarFormat:#"%Y-%m-%d"
timeZone:nil
locale:nil]);
[pool drain];
return 0;
}
So, we've got weekends covered, now we need to pull in the holidays.
Pulling in some RSS feed, or data from another source is definitely beyond the scope of my post... so, let's just assume you have some dates you know are holidays, or, according to your work calendar, are days off.
Now, I'm going to do this with an NSArray... but, again, this leaves plenty of room for improvement - at minimum it should be sorted. Better yet, some sort of hash set for fast lookups of dates. But, this example should suffice to explain the concept. (Here we construct an array which indicates there are holidays two and three days from now)
NSMutableArray * holidays = [[NSMutableArray alloc] init];
[holidays addObject:addDaysToDate(start, 2)];
[holidays addObject:addDaysToDate(start, 3)];
And, the implementation for this will be very similar to the weekends. We'll make sure the day isn't a holiday. If it is, we'll advance to the next day. So, a collection of methods to help with that:
BOOL isHoliday(NSDate * date, NSArray * holidays)
{
BOOL isHolidayResult = NO;
const unsigned kUnits = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents * components = [[NSCalendar currentCalendar] components:kUnits fromDate:date];
for (int i = 0; i < [holidays count]; i++)
{
NSDate * holiday = [holidays objectAtIndex:i];
NSDateComponents * holidayDateComponents = [[NSCalendar currentCalendar] components:kUnits fromDate:holiday];
if ([components year] == [holidayDateComponents year]
&& [components month] == [holidayDateComponents month]
&& [components day] == [holidayDateComponents day])
{
isHolidayResult = YES;
break;
}
}
return isHolidayResult;
}
and:
NSDate * ensureDateIsntHoliday(NSDate * date, NSArray * holidays)
{
while (isHoliday(date, holidays))
{
// Add one day to the date:
date = addDaysToDate(date, 1);
}
return date;
}
And, finally, make some modifications to our addition function to take into account the holidays:
NSDate * addBusinessDaysToDate(NSDate * start, int daysToAdvance, NSArray * holidays)
{
NSDate * end = start;
for (int i = 0; i < daysToAdvance; i++)
{
// If the current date is a weekend, advance:
end = ensureDateIsWeekday(end);
// If the current date is a holiday, advance:
end = ensureDateIsntHoliday(end, holidays);
// And move the date forward by one day:
end = addDaysToDate(end, 1);
}
// Finally, make sure we didn't end on a weekend or a holiday:
end = ensureDateIsWeekday(end);
end = ensureDateIsntHoliday(end, holidays);
return end;
}
Go ahead and try it:
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDate * start = [NSDate date];
int daysToAdvance = 10;
NSMutableArray * holidays = [[NSMutableArray alloc] init];
[holidays addObject:addDaysToDate(start, 2)];
[holidays addObject:addDaysToDate(start, 3)];
NSDate * end = addBusinessDaysToDate(start, daysToAdvance, holidays);
[holidays release];
NSLog(#"Result: %#", [end descriptionWithCalendarFormat:#"%Y-%m-%d"
timeZone:nil
locale:nil]);
[pool drain];
return 0;
}
If you want the whole project, here ya go: http://snipt.org/xolnl
There's nothing built into NSDate or NSCalendar that counts business days for you. Business days depend to some degree on the business in question. In the US, "business day" generally means weekdays that aren't holidays, but each company determines which holidays to observe and when. For example, some businesses move observance of minor holidays to the last week of the year so that employees can be off between Christmas and New Year's Day without taking vacation.
So, you'll need to decide exactly what you mean by business day. It should then be simple enough write a little method to calculate a future date by adding some number of business days. Then use a category to add a method like -dateByAddingBusinessDays: to NSDate.
this answer is late to the party but…. I thought i might improve on above answers to determine business days by working with NSDateComponents directly of your date in a nice loop.
#define CURRENTC [NSCalendar currentCalendar]
#define CURRENTD [NSDate date]
NSInteger theWeekday;
NSDateComponents* temporalComponents = [[NSDateComponents alloc] init];
[temporalComponents setCalendar:CURRENTC];
[temporalComponents setDay: 13];
[temporalComponents setMonth: 2];
[temporalComponents setYear: theYear];
// CURRENTC =the current calendar which determines things like how
// many days in week for local, also the critical “what is a weekend”
// you can also convert a date directly to components. but the critical thing is
// to get the CURRENTC in, either way.
case 3:{ // the case of finding business days
NSDateComponents* startComp = [temporalComponents copy]; // start date components
for (int i = 1; i <= offset; i++) //offset is the number of busi days you want.
{
do {
[temporalComponents setDay: [temporalComponents day] + 1];
NSDate* tempDate = [CURRENTC dateFromComponents:temporalComponents];
theWeekday = [[CURRENTC components:NSWeekdayCalendarUnit fromDate:tempDate] weekday];
} while ((theWeekday == 1) || (theWeekday == 7));
}
[self findHolidaysStart:startComp end:temporalComponents]; // much more involved routine.
[startComp release];
break;
}
// use startComp and temporalcomponents before releasing
// temporalComponents now contain an offset of the real number of days
// needed to offset for busi days. startComp is just your starting date….(in components)
// theWeekday is an integer between 1 for sunday, and 7 for saturday, (also determined
// by CURRENTC
turning this back into NSDate, and you are done. Holidays are much more involved.. but can actually be calculated if just using federal holidays and a few others. because they are always something like “3rd monday of January”
here is what the findHolidaysStart:startComp end: starts out like, you can imagine the rest.
// imported
[holidayArray addObject:[CURRENTC dateFromComponents:startComp]];
[holidayArray addObject:[CURRENTC dateFromComponents:endComp]];
// hardcoded
dateComponents = [[NSDateComponents alloc] init];
[dateComponents setCalendar:CURRENTC];
[dateComponents setDay: 1];
[dateComponents setMonth: 1];
[dateComponents setYear: theYear];
theWeekday = [[CURRENTC components:NSWeekdayCalendarUnit fromDate:[CURRENTC dateFromComponents:dateComponents]] weekday];
if (theWeekday == 1) [dateComponents setDay:2];
if (theWeekday == 7) {[dateComponents setDay:31]; [dateComponents setYear: theYear-1];}
[holidayArray addObject:[CURRENTC dateFromComponents:dateComponents]];
[dateComponents release];
I took #steve's answer and added a method to calculate the days of all the federal holidays in USA and put it all in a Category. I've tested it and it works nicely. Check it out.
#import "NSDate+BussinessDay.h"
#implementation NSDate (BussinessDay)
-(NSDate *)addBusinessDays:(int)daysToAdvance{
NSDate * end = self;
NSArray *holidays = [self getUSHolidyas];
for (int i = 0; i < daysToAdvance; i++)
{
// Move the date forward by one day:
end = [self addDays:1 toDate:end];
// If the current date is a weekday, advance:
end = [self ensureDateIsWeekday:end];
// If the current date is a holiday, advance:
end = [self ensureDateIsntHoliday:end forHolidays:holidays];
}
return end;
}
#pragma mark - Bussiness Days Calculations
-(BOOL)isWeekday:(NSDate *) date{
int day = (int)[[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date] weekday];
const int kSunday = 1;
const int kSaturday = 7;
BOOL isWeekdayResult = day != kSunday && day != kSaturday;
return isWeekdayResult;
}
-(NSDate *)addDays:(int)days toDate:(NSDate *)date{
NSDateComponents * components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate * result = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0];
return result;
}
-(NSDate *)ensureDateIsWeekday:(NSDate *)date{
while (![self isWeekday:date])
{
// Add one day to the date:
date = [self addDays:1 toDate:date];
}
return date;
}
-(BOOL)isHoliday:(NSDate *)date forHolidays:(NSArray *)holidays{
BOOL isHolidayResult = NO;
const unsigned kUnits = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents * components = [[NSCalendar currentCalendar] components:kUnits fromDate:date];
for (int i = 0; i < [holidays count]; i++)
{
NSDate * holiday = [holidays objectAtIndex:i];
NSDateComponents * holidayDateComponents = [[NSCalendar currentCalendar] components:kUnits fromDate:holiday];
if ([components year] == [holidayDateComponents year]
&& [components month] == [holidayDateComponents month]
&& [components day] == [holidayDateComponents day])
{
isHolidayResult = YES;
break;
}
}
return isHolidayResult;
}
-(NSDate *)ensureDateIsntHoliday:(NSDate *)date forHolidays:(NSArray *)holidays{
while ([self isHoliday:date forHolidays:holidays])
{
// Add one day to the date:
date = [self addDays:1 toDate:date];
}
return date;
}
-(NSArray *)getUSHolidyas{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy";
NSString *year = [formatter stringFromDate:[NSDate date]];
NSString *nextYear = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:(60*60*24*365)]];
formatter.dateFormat = #"M/d/yyyy";
//Constant Holidays
NSDate *newYearsDay = [formatter dateFromString:[NSString stringWithFormat:#"1/1/%#",nextYear]]; //Use next year for the case where we are adding days near end of december.
NSDate *indDay = [formatter dateFromString:[NSString stringWithFormat:#"7/4/%#",year]];
NSDate *vetDay = [formatter dateFromString:[NSString stringWithFormat:#"11/11/%#",year]];
NSDate *xmasDay = [formatter dateFromString:[NSString stringWithFormat:#"12/25/%#",year]];
//Variable Holidays
NSInteger currentYearInt = [[[NSCalendar currentCalendar]
components:NSYearCalendarUnit fromDate:[NSDate date]] year];
NSDate *mlkDay = [self getTheNth:3 occurrenceOfDay:2 inMonth:1 forYear:currentYearInt];
NSDate *presDay = [self getTheNth:3 occurrenceOfDay:2 inMonth:2 forYear:currentYearInt];
NSDate *memDay = [self getTheNth:5 occurrenceOfDay:2 inMonth:5 forYear:currentYearInt]; // Let's see if there are 5 Mondays in May
NSInteger month = [[[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:memDay] month];
if (month > 5) { //Check that we are still in May
memDay = [self getTheNth:4 occurrenceOfDay:2 inMonth:5 forYear:currentYearInt];
}
NSDate *labDay = [self getTheNth:1 occurrenceOfDay:2 inMonth:9 forYear:currentYearInt];
NSDate *colDay = [self getTheNth:2 occurrenceOfDay:2 inMonth:10 forYear:currentYearInt];
NSDate *thanksDay = [self getTheNth:4 occurrenceOfDay:5 inMonth:11 forYear:currentYearInt];
return #[newYearsDay,mlkDay,presDay,memDay,indDay,labDay,colDay,vetDay,thanksDay,xmasDay];
}
-(NSDate *)getTheNth:(NSInteger)n occurrenceOfDay:(NSInteger)day inMonth:(NSInteger)month forYear:(NSInteger)year{
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = year;
dateComponents.month = month;
dateComponents.weekday = day; // sunday is 1, monday is 2, ...
dateComponents.weekdayOrdinal = n; // this means, the first of whatever weekday you specified
return [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
}
#end