HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Caching data on disk in iOS

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
cachingdatadiskios

Problem

I've got an app that's got an activity/timeline like view. Since I don't want to retrieve the entire timeline every time, I'm caching all the events.

Currently, this is how I go about it:

```
  • (void)saveEventArray:(NSMutableArray *)eventArray {


// save hier in bg
dispatch_async(kAsyncQueue, ^{
__block int loopCount = 0;
NSArray *copyEventArray = [eventArray copy];
NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:copyEventArray.count];

if([copyEventArray count] == 0){ // If it's an empty array to save, it won't loop
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:[[NSMutableArray alloc]init] forKey:@"events"];
NSLog(@"save is done, no events left in eventarray");
}

for (NSMutableDictionary *event in copyEventArray) {
// PFFile isn't easy to encode, but UIImage is, so whenever we encounter a PFFile, we convert it to UIImage
id imageFile = [event objectForKey:@"img"];
if([imageFile isKindOfClass:[PFFile class]]){
[imageFile getDataInBackgroundWithBlock:^(NSData imageData, NSError error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
[event setObject:image forKey:@"img"]; // the PFFile is now replaced for an UIImage
NSData *eventEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:event];
[archiveArray addObject:eventEncodedObject];
loopCount++;

if(loopCount == [copyEventArray count]){ // when done looping, save it all
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:archiveArray forKey:@"events"];
NSLog(@"save is done");

}

Solution

This doesn't help with your primary concern of disk space, but you've got a high amount of unnecessary duplication in your code, as well as what appears to be some unnecessary checking and duplication.

We effectively see this block of code three times:

if(loopCount == [copyEventArray count]){ // when done looping, save it all
    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:archiveArray forKey:@"events"];
    NSLog(@"save is done");
}


Moreover, as a result of your approach, there's actually a bug that could cause the entire method to be a wasted effort. If the last object in copyEventArray is a PFFile object, but it has an error being fetched, your array won't save to user defaults at all.

And ultimately, this is code is probably a little bit hard to follow (even for you perhaps?) In a background thread, we're iterating through an array and dispatching other background threads out (presuming getDataInBackgroundWithBlock actually does what it suggests it does).

The problem is, we're trying to asynchronously load a ton of images into the same array but then wait until we're completely done loading them all into the same array before we save that single array into NSUserDefaults.

So, with all of that said, I've got a few recommendations.

-
Have you tried storing your images purely as NSData objects? Have you compared the size of NSData versus UIImage?

-
Consider as an option compressing your downloaded images. Perhaps, realistically, it might be better if the version stored on the server is more compressed, but certainly a mobile device could compress the image. Once you have a UIImage object, you can get an NSData object for the .png representation of that image, or you could get an NSData object for the .jpg representation of that image. A .png will compress the image as much as possible without any loss. A .jpg, you can specify the compression rate.

-
I highly recommend looking into Core Data. This would greatly simplify the downloading and archiving process. You could greatly improve the rate at which you pull the images back off the disk to display. And it's probably more efficient than using NSUserDefaults.

Code Snippets

if(loopCount == [copyEventArray count]){ // when done looping, save it all
    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:archiveArray forKey:@"events"];
    NSLog(@"save is done");
}

Context

StackExchange Code Review Q#82175, answer score: 3

Revisions (0)

No revisions yet.