title: What is the most efficient method to store images and data on an iPhone app from the web?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Question 2:
  The JSON-data is an NSArray in the iPhone, and the field for "Image" is just the url to the image (www.example.com/image.png), 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?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Is this the wrong approach? Is this possible? Help is much appreciated!</p>
  
  <p>EDIT
  JSON-stuff from here and there..</p>
  
  <pre><code>#define kURL [NSURL URLWithString:@"www.example.com"] 
  
  NSData* data = [NSData dataWithContentsOfURL: kURL]; 
  NSArray *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &amp;e]; 
  NSDictionary *info = [json objectAtIndex:indexPath.row]; 
  cell.textLabel.text = [info objectForKey:@"Name"]; // ±Same for url
  </code></pre>
  
  <p>This is extracted from different places in the class, so they might not work exactly like this..</p>
  
  <p>EDIT</p>
  
  <pre><code>- (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;
  </code></pre>
  
  <p>}</p>
  
  <p>Stian.</p>