Aleksander
Aleksander

Reputation: 2815

Make UITableViewController cells not disappear when navigating away

I'm making an app that uses a UITableViewController, and fills that table view with data from a webserver.

Inside my viewDidLoad I have a method that loads data from said webserver, stores it in an array as custom objects, and then loads that into cells. This is working fine.

Problem:

However, every time I navigate away from that UITableViewController, and then back, it loads everything again. This is very unnecessary, so what I did was store a boolean in the NSUserDefaults, which keeps track of whether or not this is the first time starting the app. If it is (you just logged in), load the data from the server. If not, don't.

However, as I noticed, the tableView resets every time I navigate away from (or back to) the Controller. Also, all the arrays I stored the custom objects in are now empty, so I can't load it back from the arrays either. (Every time I navigate back to the TableViewController, it's empty)

I tried storing the arrays in the NSUserDefaults, and then just populate the tableView with that data every time, but it turns out I can't store custom objects in the NSUserDefaults.

What I want to achieve is this:

Whenever I navigate away from and back to said TableViewController (I use the SWRevealViewController), I don't want the tableView to empty out. I want all the cells to stay, that way there is no wait time between when the view is loaded and the tableview is filled.

If this is impossible, I want the second best. To store the array somewhere in the app, and then reload that data into the tableview as soon as the user navigates back to the TableViewController. This is slower than my preferred solution, but still quicker and less data-consuming than loading everything from my server.

Any help is appreciated!

Thanks.

Upvotes: 1

Views: 145

Answers (2)

drewag
drewag

Reputation: 94743

You should create a separate object that manages fetching the data from the web service and then stores it locally. This object can be created in the app delegate or wherever appropriate and passed to the table view controller. The data object should provide an array that the view controller can then use to populate the table. You can even have that data object start pulling from the web service as soon as the app opens instead of waiting for the table view controller to be displayed.

I do not recommend keeping the view in memory just to save the very minimal amount of time it takes to load up a table view (using locally stored data). Unless you are talking about thousands and thousands of entries, you will not notice a lag time in the loading of the view. If you are talking about thousands and thousands of entries, I recommend you load a few hundred at a time into the table.

As far as storing the data, the simplest might be just writing the raw response of the web request to a file. A more elegant solution would probably include creating some objects to represent the data and using NSKeyedArchiver. Keeping data stored locally is a huge topic with countless options so I recommend doing some googling on your own to find the best solution for you. You might start at these places:

If you go with the NSKeyedArchiver option, you can generate a file path by doing the following:

+ (NSString *)dataFilePath
{
    NSURL *directory = [[NSFileManager defaultManager]
        URLForDirectory:NSLibraryDirectory
        inDomain:NSUserDomainMask
        appropriateForURL:nil
        create:YES
        error:nil
    ];
    NSURL *fullURL = [cachesDirectory URLByAppendingPathComponent:@"a_file_name"];
    return [fullURL relativePath];
}

Upvotes: 1

Chomu
Chomu

Reputation: 224

You need to store all the data in cache at first time when user is calling data from server. And after that whenever user navigate and comeback to the page load data from cache.

Upvotes: 0

Related Questions