1
votes

I'm working on an iPhone app which has a news feed. This news feed is pulled from a JSON web service I've written (currently living on MAMP on my laptop).

Anyway, I use a MySQL DB to store references to my images, which are stored in the apache filesystem.

I store them in a very particular way, and this is how I store them:

Full Images: ng_(postid)_(seqid)
Thumbs: tng_(postid)_(seqid)

PostID is the unique ID that is assigned to every news post.
SeqID is an ID that is only unique for the photos for that news post.

I probably didn't make that very clear... example:

The images files in the first post might look like this

ng_1_1.jpg
ng_1_2.png
ng_1_3.jpg

The image files for the second post might look like this

ng_2_1.jpg
ng_2_2.png
ng_2_3.gif

This has worked great up till now, but I tried to see what would happen if I deleted a post, and recreated one in it's place?

Let's say we have a post called 'Old Post', which has 2 images, with a postid of 7.

It's images might look like this:

ng_7_1.jpg
ng_7_2.jpg

Let's say we deleted that post, and then created a new one afterwards, which has three images and is called 'New Post'.

It's images will look like this:

ng_7_1.jpg
ng_7_2.jpg
ng_7_3.jpg

Now, here comes the problem... If the device has viewed the old post, which was deleted, and then views this new post, they will see the first two images as the ones from OLD POST. Not the new ones.

Why? SDWebImage thinks because the URL is identical, and therefore decides to pull the cached image from disk. It doesn't even display the cached version, and then check if the image has been updated.

So, I've worked out there are two possible solutions to this:

  1. Somehow get SDWebImage to check the online image, after displaying the cached version
  2. Pass down a key in my JSON, to tell my app to wipe SDWebImage's cache (when necessary)

So, my question is, how would you go about deleting SDWebImage's cache, or making it check the server after displaying the cached version?

1

1 Answers

0
votes

I think your PostID values are not unique in your system and that causes you problems. If you had unique PostID values it would be impossible to delete a post with given ID and assign that ID value to a new post...
PostID shouldn't be reusable in my opinion - can you imagine a clerk deleting a specific order in his system, creating a new one with old ID and one day getting a call from customer that provides his order ID which is now overwritten in the system?

Other thing is that you should never ever delete cached images on client's side - be user-friendly, save bandwidth and users' data plans (check this link why that matters). However you can specify cacheMaxCacheAge for SDWebImage to get rid of old, unused images. You can also remove specific images using removeImageForKey: when for example user decides to delete a specific post on his device.

Finally, the case you're describing relates more to updating a specific post, so posts can get different image set for example. In that scenario the simplest thing you can do is to use unique images IDs, so when a post is downloaded, new images will be downloaded (old ones will be deleted when cache reaches its max age - look for cacheMaxCacheAge). Alternatively, you could introduce a kind of synchronization mechanism in your DB/JSON (e.g. based on timestamps: if a post is downloaded and has a newer timestamp than the post stored in application cache, you remove old resources and download new images, text, etc... If timestamps are equal you're good with data you already downloaded).

An advanced solution would use RestKit and Core Data which would enable users to browse your posts offline and update content (images, text) when your web resource (JSON) changes.

What an epistle... I just hope my comments are useful for you :)