Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

8 things that will save your time working with Xamarin and Rails (Or the summary of my presentation about mobile development with Xamarin at TDC Porto Alegre 2017)

Posted on 2017-11-13 by Rodrigo Urubatan

In the last “The Developers Conference Porto Alegre 2017” I presented a talk about common pitfals found while creating a mobile application using the Xamarin.Forms mobile platform.

If you want to check the slides they are in my slideshare, just Click Here.

But right to the point, the idea of this post is to summarize the 8 main points in my presentation that will save your time.

The best REST client for Xamarin mobile is FubarCoder.RestSharp

I’ve tested some other libraries, and there are some good ones, but the best one I’ve found is the FubarCoder.RestSharp.

The library is almost a full port of RestSharp to Xamarin Mobile, of course, if you are writing a .Net Core application use the full blown RestSharp, but for mobile, this port is great and keep the same API

this.client = new RestClient("http://address:port/");
var request = new RestRequest("sessions.json", Method.POST);
request.AddJsonBody(new { session = new { username = Username, password = Password } });
var response = await client.Execute(request);

A REST client will not solve all your problems if you use session

In most Rails applications, you use session to keep track of who is logged in, and if you are creating a front end for one existing application and using the already existing API this can be a problem since Rails use cookies to track the user session and RestClient does not store or send cookies, but that is easy enough to solve, just use this:

var cookieContainer = new System.Net.CookieContainer();
client.CookieContainer = cookieContainer;

Adding a cookie container to the RestClient will teach it to store and send back the cookies, and that will solve all problems with authentication.

Always use async/await

C# has a perfect feature to help handle asynchronous calls, the async and await keywords, the trick here is that await can only be used inside async methods, and to solve that, use this snippet:

Task.Factory.StartNew(async () =>
{
var articles = await clientApi.ListArticles();
})

With the task factory you’ll be able to create async blocks to handle your API calls.

You cannot update the UI from another thread

The problem with async blocks is that they do not run in the main UI thread, and you can only update the UI components from that thread.

To solve that you need to use Device.BeginInvokeOnMainThread, we can update the previous snippet with this new method to update the UI with the article list:

Task.Factory.StartNew(async () =>
{
var articles = await clientApi.ListArticles();

Device.BeginInvokeOnMainThread(() =>
{
this.ArticlesList.ItemsSource = articles;
});
});

Rails tries to prevent simple form submission to prevent form submission attacks and that might be a problem

Rails protect_from_forgery will cause you some trouble, and you have two ways to fix that.

  • simply remove protect_from_forgery, of course that might open your application for attacks. If you are creating one API it is not a problem, but be careful with this solution.
  • add “skip_before_action :verify_authenticity_token” to the APIs you’ll use from your mobile application
  • before sending any “form” do a request and get the authenticity_token from the returned page and send it back on every request (this is the safest)

Show details and navigation

Each mobile platform has it’s own way to display the in app navigation and go back to the previous pages, to help with that, Xamarin.Forms has the NavigationPage class

MainPage = new NavigationPage(new tdc2017poa_xam.MainPage(this.clientApi));
Navigation.PushAsync(new ArticlePage(this.clientApi, (e.SelectedItem as Article).Id));

Saving user preferences in a platform independent way is probably a good idea

In every application you need to save user preferences and similar things, and of course that is platform dependent, but Xamarin.Forms had a Properties helper that will map the calls to each supported platform, the code is really simple, just use these classes:

  • Application.Current.Properties
  • Application.Current.SavePropertiesAsync
  • Application.Current.Properties.ContainsKey

Display notifications for users

Again each platform has it’s own requirements to send async notifications to the users, if you are using Azure to build the backend for your application, just use the Microsoft.Azure.Mobile.Client class/module/library and it will save you loads of times.

Of course you’ll still need to register your application in each platform, but you’ll be able to use only one API for all…

Last but not least

I hope this presentation summary will help you save some time, if you want to check the actual source code for the presentation samples, just go to my github page.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme