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
Related
There are several apps that I use on my Mac that store their data in core data. I can see the data I want in CoreDataPro. I want that data - specifically I want to send changes in that to some remote end points (such as Zapier, or some other REST service).
I was thinking of piggybacking something like RestKit - such that I provide a configuration file saying where the app is and what end points the data needs sending to. I need only to scrape the data and send to REST, not a two-way sync.
I'd prefer a utility that I could configure rather than having to code a Mac application.
I noted http://nshipster.com/core-data-libraries-and-utilities/ - RestKit still seemed the most capable, but in https://github.com/RestKit/RestKit/issues/1748 I was advised that coredata projects should only be opened by a single application at a time, and really RestKit is designed for baking into the source app (rather than for database scraping and sending).
What approach would you take?
I also noted:
http://www.raywenderlich.com/15916/how-to-synchronize-core-data-with-a-web-service-part-1
Thanks, Martin.
First, Core Data is an object store in memory. What is written to disk from Core Data can be in one of several formats. One of those formats happens to be SQLite. If the application is writing to SQLite then it is possible to sample that same file and push it somewhere else.
However, each application will have its own data structure so you would need to be flexible in the structure you are handling.
RestKit has no value in this situation as you are just translating objects into JSON and pushing them to a server. The built in frameworks do that just fine.
There is no utility to do this at this time. You would need to write it yourself or hire someone to write it.
If I were going to do something like this, I would write it using Core Data itself interrogate the model from the application that wrote the data in the first place and then translate the database into JSON and push it. You won't be able to tell what is new vs. old so the server will need to sort that out.
Another option, since you can't diff anyway, is to just push the sqlite file to the server and let the server parse through it.
Other answers might include:
use a middleware platform e.g. using rssbus.com (only) sqlite connections are free to send the events
as my target system (http://easy-insight.com) actually has a transmitter that sends new records it sees from MySQL abd PostgreSQL, I could https://dba.stackexchange.com/questions/2510/tools-to-migrate-from-sqlite-to-postgresql or use an ETL such as http://www.easyfrom.net (I did ask the vendor for SQLite support a long time ago, but SQLite is just not a priority for them).
I'm wondering whether a good answer (where good excludes Objective-C and includes languages that I do know, such as - to a limited extent - Ruby) is to use MacRuby and its Core Data libraries.
Core Data seemingly can be exposed as an Active Record. https://www.google.com/search?q=macruby+coredata , notably http://www.spacevatican.org/2012/1/26/seeding-coredata-databases-with-ruby/
However, MacRuby seems to have faded - https://github.com/MacRuby/MacRuby/issues/231 - it won't even compile on Mavericks.
I'm about to start an iOS project that requires pulling user's data from an SQL Database and viewing it within the App. Before I begin I'm looking for conformation that I'm taking the right (best) route.
My Plan:
App starts on login page (app will display data from another service)
App uses AFNetworking to post request to web service
Web service gets user data from SQL Database and sends back JSON
App uses JSONKit to parse the feed and load into Core-Data
App uses info from core-data to populate UI
Does this seem like an appropriate way to get the info into Core-Data from SQL? Any suggestions for doing things differently?
Thanks.
Are you receiving the response from the web server in JSON? If so, the fact that the server is using an SQL database is immaterial. What you need to know is how to parse JSON for inclusion in a core data store. Cocoa is my Girlfriend has a pretty good tutorial up.
Part 1
Part 2
To answer your comment, here's what I've done.
Display a login screen. The login credentials should be stored in the keychain for security. I've used SSKeychain for this.
To handle sending and receiving data from a web request your best option is to use a pre-built library. I've always used ASIHTTPRequest, but since it is no longer under active development, you should probably look around a bit before you commit to anything. I'm sure there are nicer and cleaner libraries out there.
You need to parse the JSON responses. I'm a fan of JSONKit. It's very fast, very easy to use, very robust.
Pulling data out of the core data store and displaying it in the interface will be no problem for you. If you create a new project in Xcode most of the setup will be done for you.
Now, there are a lot of projects out there that attempt to combine web requests, json parsing and core data loading into one framework. I've tried to use a few of these and haven't had much luck. The ones I've tried haven't been robust and very difficult to debug. Setting up your own request/parse/load code is not difficult at all, just a bit time consuming.
I am sure that there are a lot os ways to make implement this problem. Your solution is one of the popular solutions I guess but you could connect to the DB via a socket and talk with the database directly e.g. Going over a port 80 web site has the advantage that the possibility of some kind of firewall blocking the communication is very low. I would solve this kind of problem the same way I guess.
I am writing an application where we communicate with a web service in json. I would love to cache data from this service, so that the user always can show cached data while we are fetching updated data.
I have looked into a minor project; Core Resource that is a layer over Core Data. It converts from JSON to data objects, and it looks very promising. Since this project does not have a large community or high progression in further development, I am not sure if it is smart to use for me.
Do anyone know about a similar project? Or should I make it my self with just an JSON library and Core Data?
It is trivial to convert JSON to Core Data and backwards. I actually posted an answer here on Stack Overflow that walks through that.
JSON and Core Data on the iPhone
Other than that, using NSURLConnection to push and pull the data is all you really need. A "library" for this seems like an extra unneeded complexity.
An idea could be to use the URL-cache of NSURLConnection of the regular iPhone URL Loading System which supports caching on URL level. This will work especially in context of RESTFUL webservices. You can use the API's in-build cache or implement you own chaching strategy. (see here) If you don't want to persist your cache, you wouldn't need CoreData.
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/
I have a web server which contains a list of "interesting locations". These are "hard coded" on the web server, and maintained from an administrator on the server-side. Users can't add or remove anything.
From within my app, I want to make a button "fetch locations", and the app should then contact the web server and ask for that "interesting locations" list.
I'm using Core Data and have an InterestingLocation entity in my model. The goal is to get the data from the web server and persist it on the device with Core Data, so that it is accessible even without internet connection.
My idea was to use XML on the server side and output the InterestingLocation "objects" in an XML file. There are about 100 of them only, so not really huge.
On the client side (device), maybe XML-RPC?
Would be happy about some suggestions and further information where/how to get started :)
My advice is keep it simple. On the server side, use RESTful principles to make the interface as simple as possible.
Then, all you'll need to do in your iPhone app is use an NSURLConnection to fetch the URL. NSURLConnection is a very nice, asynchronous way of downloading files from remote locations.
After that, it's a simple matter of parsing the XML or JSON and creating the appropriate Core Data objects. I generally like to do import operations like this on a separate managed object context in a separate thread. When you save the managed object context, use the NSManagedObjectContextDidSaveNotification to merge in the changes with your main managed object context.
What kind of server do you have? If the server is java based I'd recommend looking at HessianKit by Fredrik Olsson. Encode/Decode to ordinary Objective-C types and put in NSArrays and NSDictionaries will make the experience smoother.