php - What is the most efficient method to store images and data on an iPhone app from the web?


I'll try to keep this as short as possible.. I am making an app for iPhone that needs to be able to store some images from the web. The URL for the images(among other data) are stored in one table at a MySQL-server. From there, I have parsed the table with JSON, and successfully serialized this to show in a UITableView in the app. Each cell now shows the image from the imageurl-field in the table, and the title of the image as text, from the title-field in the table, both from MySQL->json.php->NSArray(json)(objectAtIndex) etc.

At this point, every time the App launches, it gets all the information from the url with JSON.php every time, and I think it downloads the image every time aswell. I feel that this is way more work than necessary, but, as the database is going to update itself irregulary based on where it gets its data, it's not good enough to just download the information once.

Question 1: I need a solution where the app "asks" the .php if it has the same exact data as the JSON shows. The most logical way I can think of is to add a field for "date" in the .php, showing which date the data was last updated, and if the app has data from the same date, nothing happens, but if the php-side date is newer than the app-side date, then it must be able to delete all stored data, and replace with the new JSON-data.

Question 2: The JSON-data is an NSArray in the iPhone, and the field for "Image" is just the url to the image (, and not the actual image. How can I download these images and save them on the iPhone until a new "version" of the data is available through JSON?

Bonus question: The method I am using now is making the UITableView lagging when I scroll. I am no pro, but I have read several places that this can be caused by leaking memory, and I have not completely understood what this means. I also read that it can be because I maybe instantiate objects for every cell, instead of once and re-use it.

Bottom line: My goal is to just have the app download all the data from the json.php once at the first app-launch, and at later launches just check if the versions are correct. If correct, no download necessary what so ever, just use the stored JSON-object containing data, and the image-files from the urls downloaded the first launch. If not correct, delete all previous stored images AND clear the json-object, and import the new updated json-object, and saving the images from the image-urls from the jsonobject somewhere on the iPhone.

Note: Updating the data (i.e adding one extra image it that is all the change that has been made) is not a solution, and not a possibility in my eyes here. When there has been an update in this database, I need ALL the information stored in the phones to clear out, and redownload all. This is because the information I put in the database, is parsed from another website, and if the new images or updates to the data contains changes of some other field, then I will much rather tweak the database to fit(through json.php) than releasing update for the app all the time.

Is this the wrong approach? Is this possible? Help is much appreciated!

EDIT JSON-stuff from here and there..

#define kURL [NSURL URLWithString:@""] 

NSData* data = [NSData dataWithContentsOfURL: kURL]; 
NSArray *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; 
NSDictionary *info = [json objectAtIndex:indexPath.row]; 
cell.textLabel.text = [info objectForKey:@"Name"]; // ┬▒Same for url

This is extracted from different places in the class, so they might not work exactly like this..


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

// Configure the cell...

info = [json objectAtIndex:indexPath.row];

cell.textLabel.text = [info objectForKey:@"Name"];

NSString* imageURL = [info objectForKey:@"Image"];
NSURL *URLimage = [NSURL URLWithString:imageURL];
NSData* imageData = [NSData dataWithContentsOfURL:URLimage];
UIImage* images = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = images;

return cell;






I assume you're communicating with your server through NSURLRequest. If so, just set the cache policy to NSURLRequestReloadRevalidatingCacheData and it won't re-download your data if it's already in the cache and up to date. This way it pretty much gets handled for you.

You'll also want to set up an NSURLCache in your app delegate somewhere.

I can't tell you what's causing your chunky scrolling if you don't post your code.




Just incase someone reads this.. When making a NSURL to fetch (and parse) a large JSON file for example. You will want to do this in the background (dispatch_get_global_queue). Then call it when you want the data (dispatch_async). Otherwise you are using the same Thread as your UI and it will freeze while performing the task..

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define JSONURL [NSURL URLWithString:@""]    

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL:JSONURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];

People are also looking for solutions to the problem: hyperlink - How do I change recursive php breadcrumbs into links?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.