Search code examples
iosruby-on-railsrubyrubymotion

iOS app with RubyMotion pulling from Rails API


I built a rails application were users can add music to their personal playlist. These playlists are only seen by the owner.

There is a "my" namespace with a playlists controller. Inside the controller I have a method to add a song to the playlist and remove.

def add
    @post = Post.find params[:post_id]
    @playlist = current_user.playlist
    @playlist.posts << @post
    redirect_to root_path, :flash => { :alert => "Added to playlist!"}

end

def remove
    @playlist = current_user.playlist
    @playlist.posts.delete(params[:post_id])
    redirect_to my_playlist_path, :flash => { :alert => "Removed from playlist!"}

end

I understand how to pull all the music through the API since it is a GET request. But how would I keep the adding and removing of songs from the playlist in sync between the iOS app and web app.


Solution

  • "in sync" is a really complicated and involved subject. If you want true offline capabilities, and true "sync", you'll need to look into various options out there. But to simply use your rails backend as the data source for your iOS app is pretty easy. Just hit the endpoints to load or manipulate the data, and make the same changes to the iOS app UI.

    Here are some phases, starting with the simplest:

    Phase 1 (reloading the whole dataset every time)

    • UIViewController is loaded
      • GET playlist (returns an array of songs in a playlist)
        • populate tableview from response
    • User clicks a song to add
      • POST playlist/song
        • GET playlist (returns an array of songs in a playlist)
          • populate tableview from response
    • User clicks a song to remove
      • DELETE playlist/song (returns true)
        • GET playlist (returns an array of songs in a playlist)
          • populate tableview from response

    Phase 2 (loading the dataset once, then handling each add/remove individually)

    • UIViewController is loaded
      • GET playlist (returns an array of songs in a playlist)
        • populate tableview from response
    • User clicks a song to add
      • POST playlist/song (returns the song)
        • add the song item to the tableview
    • User clicks a song to remove
      • DELETE playlist/song (returns true)
        • remove the song from the tableview

    Phase 3 (loading the dataset once, handling each add/remove individually, with latency compensation)

    • UIViewController is loaded
      • GET playlist (returns an array of songs in a playlist)
        • populate tableview from response
    • User clicks a song to add
      • add the song item to the tableview
        • POST playlist/song (returns the song)
          • remove the previously added item to the tableview if the request failed
    • User clicks a song to remove
      • remove the song from the tableview
        • DELETE playlist/song (returns true)
          • add the previously removed song item to the tableview if the request failed