1

This is an iOS app but I will try to make it as general as possible because I think a wide audience could have good feedback.

I have a application I am making for iOS. It has 4 main tabs in a tab bar controller. There is three main model objects. Lets say they are apple, car, and clock. The first tab needs access to all the apples from an api, the second needs all the cars, and the third needs all the clocks. The fourth tab needs access to all three lists of objects.

I have implemented the first 3 tabs already and once the user goes to the first tab it will load in all the apples, same for the other 2 tabs. But now as I am starting the fourth tab I don't want to load all the objects in again (I could but it seems wrong).

I'll go ahead and give what I think might be proper and then hopefully I can get feedback if this is correct or if there is a better solution.

Proposed Solution: A class that contains a list of apples, cars, and clocks. Then each tab will get there needed list from this class. If the list hasn't been retrieved than the class containing the lists will call the api to retrieve the needed list. So I guess an object with 3 singleton objects, is this proper code design?

So as far as code structure I would have

Model: - Car - Apple - Clock - ListHolder(Is there a better way to name this?)

Controllers: - Network Controller(To call the api)

View Controllers: -The 4 view controllers

1

2 Answers 2

1

Load them all in again … but make use of caching more generally in your application.

Now you don't have to worry about double-loading.

Congratulations: you've just written your first "scalable" application. :)

0

I would suggest to cache them somehow and the simplest way to do it is to use NSURLCache. You can create a simple wrapper above it that conforms to e.x. <Cache> protocol, so you could change your cache provider without breaking any piece of code later.

You can set up Apple's NSURLCache like this

NSUInteger cacheSizeMemory = 5*1024*1024; // 5 MB
NSUInteger cacheSizeDisk = 5*1024*1024; // 5 MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];

And then you can set it to your NSURLSessionConfiguration object

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
configuration.URLCache = [NSURLCache sharedURLCache];
configuration.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.