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");
}
}
}];
} else {
loopCount++;
NSData *eventEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:event];
[archiveArray addObject:eventEncodedObject];
if(loopCount == [copyEventArray count]){ // when done looping
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:archiveArray forKey:@"events"];
NSLog(@"save is done");
}
}
}
});
}
However, I noticed that this takes insane amounts of storage and the app grows very quickly in size when doing this. Is there a better/more efficient way for this?
1 Answer 1
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 ofNSData
versusUIImage
?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 anNSData
object for the .png representation of that image, or you could get anNSData
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
.
-
\$\begingroup\$ Thanks! That's some really useful feedback :) Do you maybe have a more specific corner of Core Data you'd like to point me to? I believe it's quite big. Also, the images are now compressed on the server, but compression means loss of quality, so even though it saves disk space, compressing images makes the image also less beatiful, right? \$\endgroup\$bdv– bdv2015年02月21日 23:50:40 +00:00Commented Feb 21, 2015 at 23:50
-
\$\begingroup\$ It depends. Some images, if saved as PNG, suddenly take up significantly less disk space, and PNG is lossless compression, so there is no difference in quality. \$\endgroup\$nhgrif– nhgrif2015年02月22日 00:39:15 +00:00Commented Feb 22, 2015 at 0:39
UIImage
object? As a .png? As a .jpg? \$\endgroup\$PFFile
is and its documentation? \$\endgroup\$