Core Data vs mysql - iphone

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

Related

Storing and retrieving data from MySQL or other databases

I've been researching now for couple of days about this matter but with no luck.
I am learning iOS development with the plan of developing iPhone app mainly. Now this app will mimic the behaviour of my PHP site.
The question: Can I interact from my iPhone app directly with my tables to load and/or insert new data? if not mySQL then which database is used to host the data for CRUD operations? JSON objects? if yes, then how?
As you can see I am really unaware of the way and hope that some light should be shed on storing and retreving data from existing database or what I need to host new database to use with iPhone.
The "best" and easiest way (for me anyway) is to use CoreData.
CoreData is a data management system. You set up some objects and relationships in it and then interact with those (use fetch requests to retrieve them and stuff).
CoreData then manages a datastore (most often a SQLite DB) which it uses to store the objects and relationships etc...
Using CoreData removes the necessity of a DBA and a lot of the complex DB logic and stuff.
A good tutorial to look at is this one...
http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
I bought this book though...
http://www.amazon.co.uk/Pro-Core-Data-Professional-Apress/dp/1430236566/ref=sr_1_2?s=books&ie=UTF8&qid=1358773284&sr=1-2
and found it extremely useful for everything from starting out to the more advanced stuff.
When used properly, CoreData makes it VERY easy to use and manipulate persistent data.
YES, You can interact from your iPhone app directly with your tables to load and/or insert new data; Check the next tutorial - interact with your MySQL/PHP server http://www.scott-sherwood.com/ios-5-uistoryboards-a-practical-example/
CoreData is good but limits to access to just iDevices.
I use couchdb, means a copy is stored in the cloud with auto syncing between cloud and devices allowing for web updates and also Android

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 get data into Core-Data from an SQL Database?

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.

Is there a a way to migrate data from Core Data to Online Database?

My app currently uses core data. I created a new version of the app that is all cloud based and the database is online. Therefore this requires user registration/accounts to access the data. The easiest thing is for me to make it a separate app, but then I lose the user base I already have.
Is there a way for me to transfer data from the iPhone's core data database to the online database? I am using https://www.parse.com/
Have a look into this github project,
https://github.com/itsniper/FTASync
Sure. To transfer data from CoreData to Parse, just create a PFObject for each row in your CoreData table, and save them to Parse. You can use saveAll to be faster. Then, you don't need the local copy in CoreData any more, so you can remove it.

Can Core Data use a Web Service as a persistence store?

I'm working on an app that uses Core Data, and I'd like to be able to code it in a way that it can use a local SQLite store or a web-based store (with an XML or JSON response schema).
Is it possible to use the exact same code for the Core Data stuff and just select the appropriate persistence store based on a user's preference?
Look at the WWDC video "Building a server-driven user experience".
You can connect to a remote store via a URL but that doesn't sound like what you want as that would support only one store for every remote user.
Really, all you need to do is setup a regular SQLite store and then add a little code to send changes to the server via the chosen method. Then you could turn the server connection on and off as needed.
That would be simplest as long as you don't have a requirement that no data be persisted on the device itself.
In theory, yes. However you would probably wanna cache the data locally in case of network issues etc.
Take a look at this project https://github.com/AFNetworking/AFIncrementalStore which doesn't really implement a Web Service backed NSPersistenceStore, but it does try to achieve what you have in mind.