Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Hani Sharabash
Hani Sharabash
756 Points

Significant performance problem loading images in Ribbit

Hi there,

The Ribbit tutorial was how I learned to interact with parse. I learned from Ribbit to load images like this:

- (void)viewDidLoad
{
    // ...
    PFFile *imageFile = [self.message objectForKey:@"file"];
    NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
    NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
    self.imageView.image = [UIImage imageWithData:imageData];
    // ...
}

I recently discovered another way of doing the same thing, which is much faster:

- (void)viewDidLoad
{
    // ...
    PFFile *imageFile = [self.message objectForKey:@"file"];
    self.imageView.image = [UIImage imageWithData:[imageFile getData]];
    // ...
}

Please correct me if I'm wrong, but I think that the first version is making an unnecessary round trip to the internet to retrieve data, even though the data is already available on the PFFile.

2 Answers

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Hi Hani,

The getData method might seem faster because it is trying to access the file from cache first and, if not found there, it will grab it from Parse. If you have a look at the PFFile.h from Parse's framework you will see the comment for the getData method:

/*!
 Gets the data from cache if available or fetches its contents from the Parse servers.
 @result The data. Returns nil if there was an error in fetching.
 */
- (NSData *)getData;

The first piece of code you have always grabs the data from Parse without any caching and it may seem slower. With Parse you can use their caching mechanism and it'll speed up your app or you can always use a 3rd party library, or create one yourself, for caching files on your device. A nice one is SAMCache written by one of the teachers here at Treehouse, Sam Soffes, found here: https://github.com/soffes/SAMCache - It's really nice and easy to use.

Hani Sharabash
Hani Sharabash
756 Points

Thank you for explaining Robert! You're right that I mistook the caching as actually being faster, when in reality both methods take the same time fetch the image from the web. It's handy to know this, thank you!