REST based service on iPhone - iphone

I have never before created a REST based service. I am about to create a simple apps for the iPhone which needs to store data and I want to consume the data by using a REST API. Do you think this is worthed or should I as well do a query to the DB? What sort of DB/language do usually iPhone developers use for their application?

the iPhone includes SQLite which can store your data locally. If you need to query a system on a server, then you need some method of exposing your data and performing operations against it. Using a RESTful api with something like JSON is a relatively common method for this.
For the database, it depends on your needs, if you need a true relational database then something like MySQL, PostgreSQL, SQL Server, Oracle, etc. will work. You can also look at Google's BigTable or Amazon's SimpleDB to store data for non-relational data stores, and write your RESTful services on those.
This article has an example of integrating the iPhone with Flikr using their REST services and JSON.
http://iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html

I think RESTKit provides seamless integration with SQLite in addition to being a nice REST client. There's also RESTframework if you're looking for something simple to use.

Related

Core Data vs mysql

I already have a django application and am trying to develop an iPhone app. I'm using mysql as the database for the django app.
Here are some questions I was having :
Is it necessary to use Core Data for anything?
Can I create a rest api to interact with the mysql database?
If so, would there be any advantage, at any place or reason, to use Core Data in addition to mysql. For example, for an app like Pinterest, Tumblr, Facebook, etc. are they using Core Data at all? If so, why and how?
Core Data is one way to give you a local database on the phone. With only MySQL on the server, the app cannot access any data when offline. Even in an online-only app, a local cache of some of the data can be useful to speed things up.
Similar to Django,where it has and database-abstraction API that lets you create, retrieve, update and delete objects, iOS has an CoreData. What under-lies is still SQL. From the django end, you need to create an api that returns the class of objects or something. On the iOS side, you have to call this api and parse the data and save it locally using CoreData.
Hope this helps..

Developing an iOS app with a REST api backend (databased backed)

I'm developing an iPad app, which is connected to a Django Server on the backend. The server mostly is just a REST API on top of a database (this is done with TastyPi, for the record).
I'm trying to understand the best way to develop this, since I'm new to iOS.
So a few related questions:
Is there a library that simplifies the work of making "models" in your code that mirror the models on the server?
I would imagine something like Django's ORM, which allows you to define objects in Objective C , that are mapped 1-to-1 to objects that the REST Api gives you.
This library could abstract all of the cache-ing and converting between local objects and the objects on the server.
If this kind of library doesn't exist, are there a set of best-practices for this type of project? For example, should I even have local objects that reflect the DB? Should I have one class which takes care of all the code that deals with the API, or should I write the requests in the many different objects that are part of the API?
In short, where can I learn the "right" way to code iOS apps backed by a REST Api exposing a database? Preferably a tutorial, rather than looking at existing projects' code.
1) For ORM, iOS has Core Data that lets you build your entity and work with objects rather than SQL statements like SELECT, LEFT JOIN etc.
Don't know about others, but this is how I usually do it:
1) App makes a HTTP POST request to the Web Service using a library like ASIHttpRequest library. (Note, for the backend, I wrote my web service using Symfony web framework)
2) The app sends back the JSON response.
e.g.
{
data
{
name: bob
age: 20
}
}
3) Parse the JSON using a JSON parser like JSONKit or the one provided by ASIHttpRequest and convert the JSON server response into a NSDictionary.
NSDictionary *data = [[request responseString] objectFromJSONString];
4) Now whether to store the data on the app or not depends on the nature of the app. If the app is to do searches for local restaurants, then you probably don't want to keep a local copy of the returned result, since the nature of the app is to search for restaurants.
However, if you got like a login system that downloads user's home work from their account, then you would likely store these data on the device locally.
This is where Core Data comes in, you build your model that replicates the server model and you do a simple 1 to 1 mapping between server and client models.
Hope that helps.
Check out Rest kit
RestKit is an Objective-C framework for iOS that aims to make
interacting with RESTful web services simple, fast and fun. It
combines a clean, simple HTTP request/response API with a powerful
object mapping system that reduces the amount of code you need to
write to get stuff done.
It also supports persisting remotely loaded objects directly back into a local store
The Parse.com api is RESTful, and takes care of a kajillion hours of boilerplate code construction for a database. I don't work for them, but I do like the service.
For #1, helios.io does the trick. From the docs at github,
In order to keep your data model and REST webservices in sync, you can link it to your helios application:
$ helios link path/to/DataModel.xcdatamodel
This creates a hard link between the data model file in your Xcode and Helios projects—any changes made to either file will affect both. The next time you start the server, Helios will automatically migrate the database to create tables and insert columns to accomodate any new entities or attribute

iPhone + server + production-scale

Okay, so I'm currently developing an iphone app that I plan to take into production and scale. I'm a bit lost on the whole subject.
What is better to use: core data or sqlite? (as the local DB)
Also, can sqlite be used exclusively to communicate with my remote server as well? At first I thought it could but I've been reading that sqlite isn't great to use on servers that get a massive amount of hits.
I've read that oracle, mysql, or mssql may be better to use on a remote server and that I can communicate with these servers via REST or SOAP.
I plan to be able to both read and write to a remote server. The files transferred will mostly be small data objects and pictures. Speed is of the essence, so I'd like to know which options are my fastest routes. Of course, I want the option to scale and not have performance take too much of a hit as well.
On the subject of Core Data vs sqlite see this question.
SQLite is a small and lite embedded SQL database engine. It's not meant to used in server environments. In general, it's not a good idea to communicate directly over the Internet. It's more common to have some sort of process logic between the client code and the database to do a range of things like validate input, process business logic, security, etc. You can implement this sort of layer in REST, SOAP, or whatever you like. Since your clients will be mobile devices, a http based web service (like REST or SOAP) is a good idea as all mobility platforms have inbuilt API support for http messaging. There are lots and lots of options on the server depending on what type of server you want to setup and run with.
If your new to this, maybe you should read something like 'Patterns of Enterprise Application Architecture' by Martin Fowler to get a idea of what sort of design patterns people use to implement the server side layering.

use custom core data persistent store for getting data via webservices?

Is this a good idea? When is it a good idea, and when is it bad?
Just heard about this in one of the WWDC videos, and I don't quite understand why would one want to do it this way. Seems complicated and I cannot see the benefit.
The way I see it, it would be to totally abstract the data access layer. You'd then be able to access the web service using Core Data fetch request API. You'd also be able to implement caching in the persistent store without affecting the application logic.
Also changing the web service request/response format could potentially only affect the persistent store layer.
I see it can be a benefit for large requests. Since networking is quite expensive in battery life, application should use as less bandwidth as possible so developing a single request sending more information but using Core Data to access only subsets at a time is a good design in my opinion.
Finally, I think Core Data API blends well with major ORM web framework like rails or django for example.
It is complicated and it is meant to show what you can do with Core Data. I personally like to keep server communication separate from the local cache and then update the server based on changes to the local cache. This means that I use code that listens for save events from Core Data and then updates the server.

Online MySQL database in Objective-C

I'm writing a game for iPhone, and I want an online leaderboard using mySQL, which i'm very familiar with.
How do I implement this in my app?
I would assume there's a framework/library i need to obtain?
You don't.
You most certainly DO NOT want to expose and publish your database connection to the "live" internet. That's simply folly.
The database listeners simply aren't designed to work over such an unrestricted domain. They tend to live sheltered lives.
Instead, you should front your database with another service that IS designed for the wilds of the internet. This service can handle the authentication, encryption, load balancing, etc. requirements that good internet servers support.
Most folks today use some variant of web service, posting XML or JSON, but you can do whatever you want.
But don't open the DB connection to the live internet. You're just asking for trouble.
You should abstract the DB technology from the app, wrap your MySQL DB with a simple web service and run it on a web server, then you can use standard HTTP requests to interact with your database from your app.
My suggestions (not by any means the only way to do this)...
Use Django (or some other simple web framework) to wrap your database model, you can ever have django generate the code from your existing DB schema.
Write a few basic views to modify your DB using basic HTTP POST calls and send the username and score data in the POST data
Write a few simple pages that return the data you want in an XML format that you app can parse and display however you want, these are essentially just very simple generated web pages.
Now you have a publicly accessible leaderboard server that your app can interface with by posting scores and retrieving data through simple socket HTTP calls.
This may be outside your scope, but have you considered using OpenFeint instead to do Leaderboards and more? There's no actual framework/library from Apple to create a leaderboard in GameKit. You have to write one yourself from scratch. Although, using the OpenFeint library would give you all this for you, but that's if you want to use it.
More information here on a tutorial to do just what you're asking.
http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/