How to communicate with web service in JSON and Core Data? - iphone

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.

Related

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

how to fetch data from webserver and store to our database through SQLite

i want to fetch data from web server and save it in my local database through sqllite.How is it possible.
You can fetch data from webserver using JSON and webservice and then parse the JSON Response string.
Then you may create insert statements from the data obtained and execute it to insert in your database using executeNonQuery method of SQLite.
Best option is to do all this on appDidFinishLaunching so that there would be initial delay but then whole app after that would not face any performance issues i.e. No delays
Hope this helps you.
Well,
it depends on what kind of data you want to fetch. If you want to fetch XML from the webserver, you will get a nice list of xml parsers on this site: http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
Another nice option for fetching any kind of data from the webserver is curl. You can find static libraries on this page: http://code.google.com/p/ios-static-libraries/
Well, there's always the option to use the iOS native libraries, which are documented (with examples) in the iOS SDK.
I would not recommend using "raw" SQLite these days unless you have an exceptional reason to do so. Core Data is the way forward.
I'm not really sure what your question is; it's so broad. But, in general, you'd create an NSURLConnection, download the data, parse the data and store it in your database. Each stage is pretty well documented and there are lots of questions here on each stage.
If you use Core Data there are also some open source projects, such as RestKit, that might give you a good starting point.

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.

Using core data with web services

I am a newbie in Xcode. I am developing an iPhone app where I need to send and receive data from a web service. And I need to store them temporarily in my app. I don't want to use SQLite. So I wondering if I should use core data for this purpose. I read some articles but I still don't have a clear picture of How to do it, because I have used core data only with SQLite. I want to do the following things :
How to receive table data from a web service?
Have to perform certain calculations on those fields.
How to send the data back in xml format to the server?
How do I convert the xml data into int, date or any other data type? And How do I store it in managed data objects?
You want to use an XML parser to turn the XML into other objects; I tend to recommend TouchXML for that.
You can use Core Data and an in memory store if you are not going to save the data as it will then create and manage all of the data objects for you and generally give you less code to write. However that depends on your app if it is worth it or not. Personally i use Core Data in every app that works with data.
As for sending data, you can use the same library that you used to consume the XML to produce XML. Most of them now days are bi-directional.
For a specific example of fetching XML from server and then storing on the device using core data take a look at Björn Sållarp's blog post on Core Data and UITableView. A drill-down application. Note this example makes use of NSXMLParser and not TouchXML.
I have also found [coredatalibrary xcode template][2] to be quite useful in getting started with a new Core Data project.
Also if you are adding custom logic to your managed objects take a look at rentzch's [mogenerator][3] which generates 2 classes _MyEntity and its subclass MyEntity.

Core Data - Backing up to Google App Engine (iPhone)

I am considering backing up data from an iPhone application using the Google App Engine (GAE) - I was also considering using Python to build a RESTful app to deal with incoming/outgoing data.
On the client side I am using Core Data to store the information I wish to back up, and retrieve using the GAE.
I was wondering whether there were any good tutorials/resources on carrying out the above or whether this is perhaps something that others have tried to implement.
Any advice, or pointers, would be most welcome.
An open-source implementation of a REST server for GAE-python is available here.
I know nothing about core-data, but I you could easily store the objects in GAE if you are able to serialize them as binary or xml.
Binary objects up to 1Mb can be stored as BlobProperty, and strings as TextProperty.
There is also a Blobstore API for objects up to 50 megabytes.
If you want to store your data on a server (or sync it) then you want to go through a intermediary format. I personally recommend JSON as it can be used with Core Data easily. Since you can retrieve a dictionary of all the values in an object it is trivial to convert that diction to JSON data and push it over the wire to your server. Your server can then retrieve that JSON data and translate it into whatever format the server wants to store it in.
Do you want to map your core data objects onto GAE datastore objects? If so, this could be tricky. As you say, you would have to implement the server logic with python or Java, and you r iPhone objects are in Objective-C. You would need some scheme to serialize / deserialize them.
An easier approach, if all you wanted GAE for was backup, would be to serialize the Core data objects and store them as blobs with key-value pairs in GAE.
I'm not aware of any similar approaches so I'll be keeping an eye on this post.