Ok, so here’s the situation. I’m making a program where you can save and load documents that contain text and images (some pretty large images). For the backend, I’m using CoreData with a UIManagedDocument and my own subclass of (let’s call this NotificationDocument) of NSManagedDocument.
Right now each NotificationDocument has two attributes representing which save slot’s its saved in and what it’s saved name is. I have a TableView which is displaying a list of my documents and which save slot they are in (ex: “Save Slot 1: Cleaning Template”).
However, it takes FOREVER for me to populate this tableview. What I am doing is in viewDidLoad is:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"NotificationDocument"];
NSArray *notifications = [self.database.managedObjectContext executeFetchRequest:fetchRequest error:nil];
It seems logical to suspect that my program is running so slow because I am retrieving all of the notificationDocuments in their entirety and moving them into memory. Is there a better (faster) way to do this, given that I really don’t need all the notificationDocuments, I only need their save slot and save name attributes. What would be the best way to accomplish this?
I have thought about also storing in CoreData a map entity mapping save slots to the names saved in them. However, if there was a good way to do this without saving an entirely separate entity, that would be preferable. Thanks.
You should be using an
NSFetchedResultsControllerto populate yourUITableViewdynamically. That way, it will make the Core Data calls dynamically as new cells are requested rather than your current method, which loads all the data at once even though most of it is not needed upon loading the view.Here is roughly the code you should have to set up the data (fetchedResultsController should be a property of the view controller it is in)
And here is the line to grab the correct document in
cellForRowAtIndexPath(obviously might not be the correct object class)Do it this way and you’ll notice a huge speed improvement.