cellForRowAtIndexPath:
should only give back the the requested cell and do nothing else. Your data will not be complete if you create the CSV file inside of that method because that method will only be called for the visible cells. If you have 1000 entries and your user never scrolls to the bottom, your file will be incomplete.
You should create a designated method to export your CSV, like so:
- (void)exportCSV
{
NSMutableArray *results = [[NSMutableArray alloc] init];
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"PunchItem" inManagedObjectContext:moc]];
[request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"punchitemRoomNumber" ascending:YES]]];
NSError *error = nil;
NSArray *punchItems = [moc executeFetchRequest:request error:&error];
if (error) {
NSLog(@"Error fetching the punch item entities: %@", error);
} else {
for (PunchItem *punchitem in punchItems) {
[results addObject:[NSString stringWithFormat:@"%@ ,%@ ,%@", punchitem.punchitemRoomNumber, punchitem.punchitemRoomName, punchitem.punchitemDescription]];
}
}
NSString *resultString = [results componentsJoinedByString:@" \n"];
}
Done.