I have requirement to search events added to iOS calendar with title OR notes. I like to search events with [NSPredicate predicateWithFormat:#"title like 'Callback'"].
When googled I got predicateForEventsWithStartDate only. How can we fetch/search events with title OR notes in iOS.
You need 2 predicates to do this
Here a commented example :
// you init your store event
EKEventStore *store = [[EKEventStore alloc] init];
//you get the list of events
NSPredicate *datePredicate = [store predicateForEventsWithStartDate:[[NSDate date] dateByAddingTimeInterval: -86400.0] //yesterday
endDate:[NSDate date] //today
calendars:nil];
//this will return a list of EKEvent
NSArray<EKEvent *> *events = [store eventsMatchingPredicate:datePredicate];
//you create a second predicate to test on title or wethever you want
NSPredicate *textPredicate = [NSPredicate predicateWithFormat:#"title like 'Callback'"];
//here you will get the events with title like Callback
NSArray<EKEvent *> *results = [events filteredArrayUsingPredicate:textPredicate];
Related
I'm trying to create a recurrent event into calendar through JSON API, but with exclusion occurences (/date) I don't know how to do it. I think i create a recurrent event, then I delete occurences.
update:
For example, the event repeats every weekend. However, I removed some of the weekends from the time line, creating an exception to a recurrence rule.
How can I get these excluded dates through the API?
my code:
1. CreateEvent from api json:
NSDictionary *dicEvent = [self.arrayEvents objectAtIndex:index];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [dicEvent objectForKey:#"RecurrenceRule"];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
event.startDate = [dicEvent objectForKey:#"startDate"];
event.endDate = [dicEvent objectForKey:#"endDate"];
EKSpan span = EKSpanFutureEvents;
[self.eventStore saveEvent:self.savedEvent span:span commit:YES error:&error];
2. then with exclusion occurrences, I think I delete occurences.
EventStore *eventStore = [[EKEventStore alloc] init];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars];
NSArray *results = [eventStore eventsMatchingPredicate:predicate];
for (int i = 0; i < results.count; i++) {
EKEvent *event = [results objectAtIndex:i]
if ([event.occurencesIdentifier isEqualToString: eventoccurencesIdentifier]) {
// delete occurencesIdentifier
break;
}
Please help me where I had gone wrong.
Thanks!
I fetch an array of events from Core Data. In turn, I want to filter the results to obtain any events currently taking place, ie that started within the last hour.
I'm a bit fuzzy on how to filter the array and even fuzzier on how to work with dates in an NSpredicate. I think I need to calculate a time that is one hour before the present, a second that is a short time from the futrue, and then compare these the starttime but I'm not sure how to go about it.
This is what I have so far:
//fetch from coredata
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSMutableArray *mutableresults = [results mutableCopy];
NSDate* now = [NSDate date];
NSTimeInterval backSecondsInHour = -60 * 60;
NSTimeInterval forwardSecondsInHour = 60 * 60;
NSDate* hourAgo = [NSDate dateWithTimeInterval:backSecondsInHour sinceDate:now];
NSDate* hourFromNow = [NSDate dateWithTimeInterval:forwardSecondsInHour sinceDate:now];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"starttime >= %#&&starttime<=%#",hourAgo,hourFromNow];
NSArray *filteredEvents=[mutableresults filteredArrayUsingPredicate:predicate];
Am I on the right track here? Would appreciate any suggestions.
Instead retrieving all events every time from database just fetch only needed events...
Here is example if predicate applied to fetchRequest for all events since last hour:
NSDate *hourAgo = [[Date date] dateByAddingTimeInterval:-3600];
NSFetchRequest *fetchRequest = .....
request.predicate = [NSPredicate predicateWithFormat:"starttime >= %#", hourAgo];
Following is the code I'm using to fetch the events from Calendar, but I always get the array nil.
EKEventStore *eventStore = AppContext.HSCalendar.eventStore;
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:[NSArray arrayWithObject:[eventStore calendarWithIdentifier:AppContext.HSCalendar.selectedCalendarID]]];
NSArray *arrEvent = [eventStore eventsMatchingPredicate:predicate];
How can I get all the events from the custom calendar?
I am trying to list out the Name, Location, and Notes from calendar events. Reading and writing the Name and Notes work as expected but I run into problems with the Location field.
Specifically, the following line "meetingLocation = [element location];" produces the error
"Multiple methods named 'location' found with mismatched result, parameter type or attributes."
What is wrong here? Code is included below.
-(IBAction)reloadEvents:(id)sender {
NSString *meetingName;
NSString *meetingLocation;
NSString *meetingNotes;
// Define a range of event dates we want to display
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:(-1*60*60*.5)]; // .5 hour in the past
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:(60*60*24*1)]; // 1 day from now
//NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:(60*60*24*7)]; // 7 days from now
// Create a predicate to search all celndars with our date range using the start date/time of the event
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
// Query the event store using the predicate.
NSArray *results = [self.eventStore eventsMatchingPredicate:predicate];
// Convert the results to a mutable array and store so we can implement swipe to delete
//NSMutableArray *events = [[NSMutableArray alloc] initWithArray:results];
//self.events = events;
NSEnumerator * enumerator = [results objectEnumerator];
id element;
while(element = [enumerator nextObject])
{
// Set the meeting name
meetingName = [element title];
NSLog(#"Name=%#",meetingName);
// Set the meeting location
meetingLocation = [element location];
NSLog(#"Location=%#",meetingLocation);
// Set the meeting notes
meetingNotes = [element notes];
NSLog(#"Notes=%#",meetingNotes);
}
}
Try like this
while(element = [enumerator nextObject])
{
EKEvent *event = element;
meetingName = event.location;
}
Similar problem converting some old iOS 5 to iOS7:
if ([[thisEvent.recurrenceRules objectAtIndex:i] frequency] == EKRecurrenceFrequencyDaily ){
resulted in
Multiple methods names "frequency" found with mismatched result.
Resolved by typecasting and then doing the if statement
EKRecurrenceRule *thisRecurranceRule = (EKRecurrenceRule *)[thisEvent.recurrenceRules objectAtIndex:i] ;
if ([thisRecurranceRule frequency] == EKRecurrenceFrequencyDaily ){
I'm attempting to get a list of to-do lists that have to-dos >= a certain date.
Data Model: Todolist has many Todos.
The due_at field in Todo model is a Date object.
This is the fetch request I'm using and it keeps crashing.
NSDate *now = [NSDate date];
int daysToAdd = _dueDateSlider.intValue;
NSDate *datePeriod = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Todolist"];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"todos.due_at >= %#", datePeriod];
[request setPredicate:pred];
NSError *error = nil;
NSArray *todolists = [managedObjectContext executeFetchRequest:request error:&error];
The error I am receiving is:
-[__NSArrayI compare:]: unrecognized selector sent to instance 0x1018a4c50
An uncaught exception was raised
I'm not sure why this is happening, as any other predicate search works fine. todos.due_at != nil is fine as well as todos.content like '%test%' works fine.
To get the lists that have any to-dos >= a certain date, use
[NSPredicate predicateWithFormat:#"ANY todos.due_at >= %#", datePeriod];