Check If Server's Image is Already Updated and Download Again - ios

I would like to know what is the proper way of caching image and store it to Parse.com, and load it back, update cache etc.
So here is the scenario:
I have a social network app where user can upload their profile picture.
Once the user upload the picture. I believe we should cache the image in the device.
User can also change their profile picture from the website.
My question is, If user update their profile picture. How could I detect the changes and update the cache? Image caching libraries detect from the URL. The problem is, the URL always stays the same.
So how do we know if the picture is already updated and re-download it to the device and replace the cache?
Thank you

You can
Set a validity period for the image cache. So image cache is reloaded with the latest data form server every, say, 24 hours or so.
Keep a 'timestamp' on your server whenever user uploads a profile picture and keep a local time stamp for every image URL on device when the image is cached. Check/compare time stamp on every app execution or every time the profile page is opened. When the server timestamp is newer, invalidate the cache and re-download the new image. Make sure local cache timestamp is updated every time the image is cached.
Maintain a file 'hash' string on the server. When you download the image file create a file hash locally and maintain it for every image URL. Compare local value to the server hash on every app execution or everytime the profile page is opened. If they are not the same, invalidate the cache and re-download the new image. Make sure local file hash is updated every time the file is downloaded. However, this will not be possible if your image caching module does not give you direct access to the downloaded physical file.
I assume you have clear idea of an image caching stratagy and hope this answers your question regarding 'how do we know if the picture is already updated and re-download it to the device and replace the cache?'.
If you want to know how to cache in image, you can use UIImage+AFNetworking.

Related

Rails - save controller instance variable data across HTTP request

In my project, a user is uploading a text file, that needs to be read.
File, can be of any size, the file I am using is 1 MB and has ~1500 lines. The file can be bigger as well. Hence instead of putting all in db, i thought of processing the file and retaining the data in instance variable.
But instance variables are not available across HTTP request. Hence what are the options available to me to retain the instance variable values across HTTP request.The other reason for not choosing DB was, I dont need the data to be persisted. As long as user is logged in, data needs to be present for that time duration only. Once user is logged out, I can discard the data.
Please let me know if you need further information.
As #xyious advises, I would say avoid storing that much data in session, it is just not a good practice. You could, however, do the following:
Setup a system-wide configuration setting that holds a path where you store temporary files, in this case, the files uploaded by the user
Generate a random (maybe with SecureRandom.hex) filename when the user uploads the file and store this file in the path mentioned on point #1
Store this random filename in the user session, that way, even if you change between requests you can still access the filename
On each request, whenever you need to process the data, pull the filename from the user's session and join the path of the setting of #1, read the file from the filesystem and do the processing as necessary
Add a callback on your login/sessions controller so that when a user logs out you go and find the filename and delete it before logging out, that way you don't keep unused files around
I would advise against it, but you could store the data in a session variable, or in a cookie.
Why would you need that much data to be stored while the user is logged in ? Is it possible to just save important bits ?
Using instance variables to store content is not a right approach since you don't have a limit on the size of file uploaded and you end up passing the data everytime.
Firstly, decide something on the size limit since you expect text file from users and then upload the file temporarily with a reference path in DB. This file can be cleaned up when required and will make accessing the content simple. To further improve this, enable caching mechanism and setup a caching server for the uploaded files.
If you are not fine with this then other option i can think of is using session variables which is already suggested. So this data will stay per session which fits your requirement. you can just session[:file_Data] = "put parsed content here"

How do I use AWS to update my iOS App without an official app update?

So I have an app that is pretty simple. It has data stored in .htm files which can either be displayed in a UIWebView or parsed through and return a string of the results(the app does both). The data changes weekly, and obviously it takes longer than that just to review an update submission. I want the user to be able to click a button and check for updates on a server and then download/replace the .htm files as needed. The app can take it from there. What service through Amazon Web Services is the best to check for new files and download them? If you think there is an easier service than AWS than I'm definitely open to other ideas.
You could store your files in S3. Set up your app so it downloads the file from S3, then compare it with the one you've got locally (you could hash them both and compare the hashes).
If it's changed then you can use the new one.

Deleting no more used static content locked by IIS

I am working on an ASP.NET MVC application where a user can manage its own profile. He can change for example his own photo.
Since a photo is considered as static content, IIS will lock this file, as I understand, and cache it to optimise performance.
The problem happens when the user try to change the image. What I am doing is :
Record the new image.
Start serving the new one. The old file will never be served.
Now I need to remove the old image. But I having access denied exception.
How to tell IIS to unlock this old photo so that I can delete it.
One can imagine setting up a loop that tries to delete the photo and if cannot it will wait and retry... but I have no idea how much time that can take.
Do you have any better solution to tell IIS to unlock file that will never be used ?

Concurrent photo and note uploading

I have this requirement whereby the user is uploading a photo from the phone, and typing a note along with the photo
However, to vastly improve responsiveness, I would like to start uploading the photo before the user has finished typing the note (the same technique instagram uses)
So, there are 3 scenarios:
- the photo uploading finishes, then the note is submitted
- the note is submitted, then the photo finishes uploading
- the note and photo finishes uploading at the same time
I am using a unique token, as well as after_create hooks now to check and save.
a unique uuid comes along with both the photo upload and the note
store the photo in a temporary store with the uniqueid
when the photo is saved (after_create), check if there are any notes with the uniqueid. If so, attach the photo to note.
store the note with the uniqueid
when the note is saved (after_create), check if there are any photos with the uniqueid. If so, attach note to photo
This seems like a rather common problem, and I am wondering whether there are any accepted patterns / solutions around this? I am using mongoid and rails 3.2
To avoid concurrency problems, and UI confusions. What I would do, in your case, is:
User choses the photo -> set local variable in JS(client) -> servers starts uploading the photo
( when server respond back from the photo upload, unset the local js var )
User write notes
User submits the form -> you check if the js variable is set or not, if it is, means that photo was not uploaded yet, so you show a message. Otherwise you just submit the form with the note.
If you simplify your flow to be something like that, you should not have too much edgecases to have to debug and fix.
The only potential risk in here, is a 'deadlock' case, lets say for some reason server never send back a response to client, which never unset the js var. To avoid those cases you could have a big timeout on the client, that if there server dont respond within that timeout you show an error message.

Store files temporarily on server until a task is completed

I cant use sessions.
So heres the scenario: I want the user to upload an image, but that image needs to be a particular size. So I allow them to upload any size image, store that temporarily on the server (resize it so it fits on the webpage), display it back to the user, let the user crop it. I then send the crop details back to the server, crop the image and save it and use it as the users profile picture.
I tried to do all this before uploading, but apparently, its a security risk and not allowed.
So how do I temporarily store this file? What if the user does not come back before cropping, I dont want a large image like that sitting on my server. How would I go about removing the file in a stateless application like this?
Files are stored on a CDN.
There are lots of ways to solve this, but perhaps an easy way is that every time a file is uploaded, call a little routine that checks for, and deletes, any 'large' files that are over xxx minutes old.
Alternatively, schedule a job to do the same every xxx minutes in the task scheduler.
You can use TempData, which is similar to Session, but dies after being read.

Resources