How To Choose the Best Backend Provider for your iOS App: Parse vs Stackmob vs. Appcelerator Cloud and More!

This is a post by Tutorial Team Member Antonio Martínez, a mobile software developer currently working as an iOS Developer in London. It’s quite common for apps to require a web backend. This allows you to have a central database where users can share content with each other, like photos, messages, or restaurant reviews. In […] By .

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Introducing PhotoShare

To test out these three back end providers, I wanted to develop a simple but real-world application in each provider, and then compare and contrast to see the differences.

I decided to make an app called “PhotoShare”, similar in some ways to Instagram and other photo sharing apps. It has a login screen, then presents the user with a wall of images and comments. Every registered user can upload an image with a comment attached to it, and these images are publicly viewable.

In the first view, the user can Register as a new user, or Login if an account has already been created:

Once the user is logged, in the view proceeds to the list of images:

An Upload button in the navigation bar allows users to submit their own images and comments:

Next you’ll take a deeper look at Stackmob, Parse, and Appcelerator Cloud. For each provider, you’ll:

  • Get an overview of the features, pricing, and documentation.
  • Take a peek at how the code works.
  • Learn how to get this PhotoShare sample project up and running on your own account.

Let’s start with Stackmob!

StackMob: How Does it Stack Up?

Features, Pricing, and Documentation

StackMob stands out from the other services as being extremely flexible, and provides the following features:

  • Custom Objects
  • Files (served through Amazon S3)
  • Users
  • FB/TW Integration
  • Push Notifications

Update: Since writing this article, StackMob has revamped their pricing. Now most of the functionality is free, which is great! You have the option to purchase modules in their MarketPlace for additional advanced features.

You can find more info, and check what is included here:
StackMob Pricing

Well, those features are fairly impressive so far, and the documentation for StackMob is fairly complete. But ease of implementation when you’re coding is really where the rubber meets the road.

Take a look at the following code examples and see how common BaaS tasks are accomplished in StackMob!

Code Examples

The following code shows how to upload a simple object in StackMob. The example below follows on from the theoretical player application mentioned above:

NSManagedObject *newManagedObject = [NSEntityDescription  
  insertNewObjectForEntityForName:@"PlayerScore"
  inManagedObjectContext:self.managedObjectContext];
    
[newManagedObject setValue:@"Player1" forKey:@"name"];
[newManagedObject setValue:[NSNumber numberWithInt:1000] forKey:@"score"];
[newManagedObject setValue:[newManagedObject sm_assignObjectId] 
  forKey:[newManagedObject sm_primaryKeyField]];
    
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Error: %@:", error);
}
else {
    NSLog(@"Succesfully uploaded");
}

As you can see, uploading an Object using StackMob is a fairly simple process. Essentially, you create a ManagedObject with the object you wish to upload, and then simply make a call to upload it to the BaaS .

Does this look familiar? It should — StackMob backend storage functions are provided through extending Core Data’s mechanisms. Isn’t that terribly nice of them? :]

Now move on to the following sample code, which shows how to retrieve objects from StackMob:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
        
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PlayerScore" 
      inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
        
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" 
      ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
        
    [fetchRequest setSortDescriptors:sortDescriptors];
        
    //Greater than 900
    NSPredicate *greaterThanPredicate =[NSPredicate predicateWithFormat:@"score > %d", 900];
    [fetchRequest setPredicate:greaterThanPredicate];
        
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = 
      [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest   
         managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil 
         cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
        
    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"An error %@, %@", error, [error userInfo]);
    }
        
    return _fetchedResultsController;
}

// You can get the objects calling self.fetchedResultsController

This uses a standard NSFetchedResultsController to retrieve a list of objects – for more information, check out this tutorial.

Deleting objects using StackMob is probably the simplest action of them all:

[self.managedObjectContext deleteObject:aManagedObject];
     
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"There was an error! %@", error);
}
else {
    NSLog(@"You deleted an object!");
}

Pretty simple, isn’t it; there’s more lines of error handling code than there are lines to delete the object in question! :]

As you can see from the examples above, the process of uploading, querying, and deleting objects is fairly simple and adapted to work with iOS using the SDK that they provide. It’s apparent that an existing project using Core Data could be easily adapted to make use of the StackMob backend.

The image below shows how the StackMob web interface for the object browser will appear after uploading an object:

Common operations such as delete or query are available through the web interface as well. This is useful for managing objects in a manual fashion, such as setting up persistent data for your app.

Creating a Account

Before you can run the Stackmob implementation of PhotoShare, you’ll need your own StackMob account and you’ll need to configure a database for the app. This section will show you how.

If you have not already created a StackMob account, go ahead and follow the directions found at StackMob: Get Started. Signing up is easy, and you are not required to submit any information other than your email address and password. Right away, you’ll receive a welcome email and the usual “verify your account” link.

It’s a bit counter-intuitive, but when creating a new app on StackMob, make sure to choose your platform first, before you enter the app name. The downloadable tutorial examples use “tutorialstackmob” — all lower case — as the app name.

You can skip downloading the SDK — for the purposes of this tutorial, the SDK is provided in the example app, along with the necessary frameworks.

Once you’ve verified your StackMob account, you’ll now need to get an Amazon S3 account setup to store your binary assets. To do this, sign up here.

Don’t be daunted by the Amazon Web Services signup process. You do need a credit card, but there is no signup fee, and if you read the service agreement carefully, you’ll find that you will only ever be charged for data or service usage that exceeds what the free S3 tier offers.

The captcha for the account creation is a bit tricky, and you’ll also need to enter a specific PIN on an automated phone call — but at least you know you’re using a service with security! :] At the end of the process, the result is an access key, required to link your new S3 account to your StackMob account.

In order to get your S3 buckets and access setup, you’ll need to follow the two sections titled “Setup an Amazon S3 bucket” and “Add S3 settings to your StackMob application” in this tutorial. Don’t worry about the section marked “Add a Binary Field” — you’ll do that below.

Now you need to create the schemas of the objects you are going to use in your app!

Go to Manage Schemas and create a new schema called “wallobject”. Add two new fields; one field called “comment” with String type, and another one called “photo” with Binary type, as shown in the screenshot below:

Screen Shot 2012 11 01 at 5 57 48 AM

You may be prompted to set permissions before you can save your new schema. If so, “Logged In Permissions -> Allow to any logged in user” is fine for all operations. Now, save your schema!

Running the Sample Project

Now you can try out the app! Go ahead and download it from github.

Note: If you have not used github before, grabbing the sample application code is as simple as navigating to the folder where you would like it to appear using terminal, and then using “git clone https://github.com/toniomg/TutorialStackMobBaaS”.

Open the project in Xcode. If you run the project immediately, you’ll encounter an exception: “‘Incorrect Public Key format provided. Please check your public key to make sure you are passing the correct one, and that you are not passing nil.”

There’s one little piece missing to link your app to StackMob: your public key! In the StackMob Dashboard of your app, find the link to “Manage App Info”. Copy your public key to the project in the Delegate (look for a field called PUBLIC_KEY). The bit of code you’ll need to update will look like this:

#define PUBLIC_KEY @"123456-7890"

Build and run your app! If you receive an error saying that the schema doesn’t exist, try waiting for a minute and trying again — it may take a moment for the backend service to make your schema available to the outside world. However, if you still encounter difficulties, try re-creating the schema just to be sure that it has been properly created.

Congrats – you now have a working example project with StackMob! You can compare this to the other two projects yourself – or keep reading for our own thoughts and conclusions.