IPhone : Get contents of PFFile without caching

on Saturday, February 21, 2015

I'm working on an app that has a kind of 'stream'/'timeline'. In order to quickly load new data, I'm currently caching all the data the app receives.


However, I'm encountering a problems: I'm getting a PFFile with getDataInBackgroundWithBlock method of Parse, which automatically caches the content as well.


However, Since I can't write PFFile to disk, I'm also caching the data from the PFFile, like this:



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, when I do this, there are actually 2 of the same images in cache..


How do I prevent using getDataInBackgroundWithBlock? If that's the only way to go about it, there should be a way to cache the other event data without the image itself, while still being able to connect the 2 when displaying the data.


0 comments:

Post a Comment