views:

40

answers:

2

I starting to investigate whether Ruby-on-Rails will help solve my problem in some way?

In short, I have a legacy Linux system and application which is reading and writing a collection of images. This application also uses a sqlite database to help refer to correct image files when required.

What I'd like to do is have an iPhone and/or iPad application browse the images, but also be able to see updates and additional images without hitting a refresh button; or using a periodic timer say.

This brought me to investigate Ruby-on-Rails as a solution?

I made the following diagram to think about whether I'm on the right path: isrubyonrailstheanswer To date I obviously have the (legacy) in-place linux app, which generates images and updates the database. I have also embedded a bonjour service inside the linux app to allow an iPhone application to connect and to-date browse thumbnails that are inside the sqlite database. The tricky part is that I've realised that the iPhone app needed to be kicked to update its images when the database is changed remotely.

Actually I thought that with a ruby-on-rails app it could provide a RESTful service to access fullsize (plus, thumbnail) images allowing the thumbnail images to be removed from the sqlite database - it's likely the database could have 10000+ entries, so didn't want the db to grow unnecessarily.

Any tips or resources would be great.

Thanks.

Update: I had a think about the wording of my question again. I guess I was trying to figure out if I could get something like Rails to monitor changes to a database without polling in some way.

A: 

One possible answer would be to use Apple's push notification service, it would work outside your application as a bonus.

It really depends on what you want to provide in your application. You could just schedule requests from your application to your server (which could be a Ruby app), so you poll for updates. That's probably the easiest solution.

jv42
Using push notifcation is a neat idea but a typical install of the system is not open to the internet. I haven't ruled-out a polling solution where the iPhone asks for updates, but it's not ideal because changes can happen at any time. Thanks again.
petert
You can setup polling to the interval you wish. First I'd try something like 5 or 10 seconds, then tweak after testing. Or if you feel like overkill, you can do an adaptative system that tries to guess how often it should poll, based on previous poll results ;-)
jv42
+1  A: 

The simplest solutions I can think of is to have an action on your RESTful service which pulls the most recently modified date from the database. Periodically query this from your iPhone app, and then refresh the list.

For example, if you create scaffolding for images:

rails generate scaffold Image name:string filename:string # fill in the other attributes you need

NOTE: You will have to sync your legacy DB to this Rails table

Then each image object is automatically created with an "updated_at" attribute.

class ImagesController < ApplicationController
  # GET /images
  # GET /images.xml
  def index
    @images = Image.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @images }
    end
  end

  # GET /images/1
  # GET /images/1.xml
  def show
    @image = Image.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @image }
    end
  end

  def newest
    @image = Image.find(:last, :order => :updated_at)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @image }
    end
  end

  # more methods down here
end

and you'll have to add the following match line to your routes.rb

YourAppName::Application.routes.draw do
  match 'images/newest' => 'images#newest'
  resources :images
end

Now, from the iPhone app you can pull an XML file describing your most recent image with this URL

http://localhost:3000/images/newest.xml

Parse out the most recently updated time, and compare it to your last refresh date.

marshally
Wow, thanks for the guide. I'll give this a go.
petert
Your explanation has help me move forward with the problem; voting up answer, thanks.
petert