I have a very strange behavior of my UITableView. When I call it first time after starting the app all data are shown as they should. When I move to another Viewcontroller and come back to the tableview it only shows half the data. I figured out that [tableview reloaddata] in viewwillappear causes the issue. If I comment it out, all data stay as they were. Therefore I suppose that something is going wrong while retrieving the data when coming back. I need to tell that the data are coming from 2 different arrays which fill labels in a custom UITableViewCell. As I have some other difficulties with the setup of this cell (labels don't work compliant to the set constraints) first I thought maybe the labels are out of view. But now I'm pretty sure that the data of one of the arrays are gone.
Does anyone know, if there is a difference of loading data for the first time and reload data? Is it maybe a timing problem and the tableviewcontroller does not have enough time to retrieve the data? If yes what can I do against it? Could there are other reasons why it doesn't work? Please tell me if I should post some code. I did not know what to show to solve the issue.
Thank you in advance for your appreciate help.
`
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self fetchAllTermine]; //method to retrieve data from array #1
NSLog(@"viewWillAppear: %@", [self fetchAllTermine]);//logfile always shows data
events = (NSMutableArray*) [self getEventsFromCalendar]; //method to retrieve data from array #2 , events is a global variable
[self.tableView reloadData];//after reloadData data from array #1 are missing
}
-(NSMutableArray*)fetchAllTermine
{
NSFetchRequest<Termine *> *tfetchRequest = [Termine fetchRequest];
NSError *error ;
NSMutableArray * fetchedTermine = [[[self managedObjectContext] executeFetchRequest:tfetchRequest error:&error]mutableCopy];
self.termineArray = [[[self managedObjectContext] executeFetchRequest:tfetchRequest error:&error]mutableCopy];
// NSLog(@"fetchAllTermine in MonatTVC: %@", self.termineArray);
return fetchedTermine;
}
-(NSArray*)getEventsFromCalendar
{
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Access to calendar granted");
} else {
NSLog(@"No access to calendar");
}
}];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSArray *calendars = [[EKEventStore new] calendarsForEntityType:EKEntityTypeEvent];
NSDateComponents *tomorrowComponents = [[NSDateComponents alloc] init];
tomorrowComponents.month = 0;
NSDate *tomorrow = [calendar dateByAddingComponents:tomorrowComponents
toDate:[NSDate date]
options:0];
// Create the end date components
NSDateComponents *afterTomorrowComponents = [[NSDateComponents alloc] init];
afterTomorrowComponents.month = +1;
NSDate *afterTomorrow = [calendar dateByAddingComponents:afterTomorrowComponents
toDate:[NSDate date]
options:0];
NSPredicate *predicate = [[EKEventStore new] predicateForEventsWithStartDate:tomorrow
endDate:afterTomorrow
calendars:calendars];
NSArray *eventListe = [[EKEventStore new] eventsMatchingPredicate:predicate];
return eventListe;
}
`