Can't obtain reference to EKReminder array retrieved from fetchRemindersMatchingPredicate
Posted
by
Scionwest
on Stack Overflow
See other posts from Stack Overflow
or by Scionwest
Published on 2013-06-30T21:05:07Z
Indexed on
2013/06/30
22:21 UTC
Read the original article
Hit count: 529
When I create an NSPredicate via EKEventStore predicateForRemindersInCalendars; and pass it to EKEventStore fetchRemindersMatchingPredicate:completion:^ I can loop through the reminders array provided by the completion code block, but when I try to store a reference to the reminders array, or create a copy of the array into a local variable or instance variable, both array's remain empty. The reminders array is never copied to them.
This is the method I am using, in the method, I create a predicate, pass it to the event store and then loop through all of the reminders logging their title via NSLog. I can see the reminder titles during runtime thanks to NSLog, but the local arrayOfReminders object is empty. I also try to add each reminder into an instance variable of NSMutableArray, but once I leave the completion code block, the instance variable remains empty.
Am I missing something here? Can someone please tell me why I can't grab a reference to all of the reminders for use through-out the app? I am not having any issues at all accessing and storing EKEvents, but for some reason I can't do it with EKReminders.
- (void)findAllReminders {
NSPredicate *predicate = [self.eventStore predicateForRemindersInCalendars:nil];
__block NSArray *arrayOfReminders = [[NSArray alloc] init];
[self.eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
arrayOfReminders = [reminders copy]; //Does not work.
for (EKReminder *reminder in reminders) {
[self.remindersForTheDay addObject:reminder];
NSLog(@"%@", reminder.title);
}
}];
//Always = 0;
if ([self.remindersForTheDay count]) {
NSLog(@"Instance Variable has reminders!");
}
//Always = 0;
if ([arrayOfReminders count]) {
NSLog(@"Local Variable has reminders!");
}
}
The eventStore getter is where I perform my instantiation and get access to the event store.
- (EKEventStore *)eventStore {
if (!_eventStore) {
_eventStore = [[EKEventStore alloc] init];
//respondsToSelector indicates iOS 6 support.
if ([_eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
//Request access to user calendar
[_eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"iOS 6+ Access to EventStore calendar granted.");
} else {
NSLog(@"Access to EventStore calendar denied.");
}
}];
//Request access to user Reminders
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"iOS 6+ Access to EventStore Reminders granted.");
} else {
NSLog(@"Access to EventStore Reminders denied.");
}
}];
} else { //iOS 5.x and lower support if Selector is not supported
NSLog(@"iOS 5.x < Access to EventStore calendar granted.");
}
for (EKCalendar *cal in self.calendars) {
NSLog(@"Calendar found: %@", cal.title);
}
[_eventStore reset];
}
return _eventStore;
}
Lastly, just to show that I am initializing my remindersForTheDay instance variable using lazy instantiation.
- (NSMutableArray *)remindersForTheDay {
if (!_remindersForTheDay) _remindersForTheDay = [[NSMutableArray alloc] init];
return _remindersForTheDay;
}
I've read through the Apple documentation and it doesn't provide any explanation that I can find to answer this. I read through the Blocks Programming docs and it states that you can access local and instance variables without issues from within a block, but for some reason, the above code does not work.
Any help would be greatly appreciated, I've scoured Google for answers but have yet to get this figured out.
Thanks everyone!
Johnathon.
© Stack Overflow or respective owner