Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. 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.

  1. 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.

like image 660
Edan Maor Avatar asked Dec 06 '25 19:12

Edan Maor


1 Answers

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.

like image 120
Zhang Avatar answered Dec 08 '25 09:12

Zhang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!