0

When using core data, how would I insert to values into a database, or even interact with a database. Is there a good tutorial for this? I have went over Core Data tutorial, but majority the files were done locally.

UPDATE
Someone from my previous question answered this
"Core Data can use a database to accomplish this, but that's it's business, not yours. When you use Core Data, it is a black box. You tell it to save the data and then step out of the way."
Does that mean it automatically does the database side for me?

Thank you!

2 Answers 2

1

Core data is a black box in that it is an object oriented API for interacting with the SQLite db on the device (and in iCloud). If you are pushing to a web service that has a SQL database backend... Well, I've used a php controller for the web service that provides a rest interface. Then I get data from my device and hit the rest controller methods passing whatever data is appropriate as parameters in an http post. Not sure whether that's general or best practice but that's how we've done it in my experience

Sign up to request clarification or add additional context in comments.

7 Comments

Okay thanks for the reply. Okay so pretty much it will handle the database side for me when i'm using core data. So would I need to connect to a server when using core data? Thanks so much.
It depends on what you want to do. If you want to push data to a server then that's a different db from the SQLite on the device. And yeah you would need to send an NSURLRequest to your server, passing the data explicitly in a post. Core data is for interacting with the local db (and iCloud if you have that going on as well)
Oh okay, so I wouldn't be using core data if I wanted to interact with a non-local database? When you say local database do you mean by the device itself? Thank you so much geraldWilliam.
Like for example I want to make an app where users can update their address to a database. And users can view addresses of the different people. Would I use core data to accomplish this?
You could go either way. I would use core data in that case to store addresses I pull from the web service and to manage them locally. But the interaction with the web service is more a networking question than a job for core data. But hypothetically if all you wanted to do was view and post addresses you could just use post and get requests passing JSON back and forth.
|
0

Core Data is not a db manager, and does much more than simply interact with your db. At the very least, you should consider Core Data as an ORM. Perhaps, the best way to understand Core Data is to think it as a graph administrator. You are going to interact with core data via a context (NSManagedObjectContext) that will let you fetch objects from it, and also insert, edit and delete them. Objects are subclasses of NSManagedObject. One important thing to care about are threads. NSManagedObject are not thread safe, you should never pass them between threads. Use NSManagedObjectsIDs instead. The important thing, you don't need to care about how core data store things, you can set the format of the store, but that's all (in that respect, you do have to take a few things into consideration when choosing the store, like performance, and whether you need to persist data between runs, but let's Core Data handle the details).

And if you want to know more, Marcus Zarra's book is excellent

2 Comments

Alright that makes sense, but I guess my question is. If I stored something, would someone else using the app would be able to be able to access that stored value? For example If "A" wanted to store his address using Core Data, could "B" access "A"'s address. That is what I am trying to accomplish. Thank you for the reply Ariel.
By A and B you mean different apps? then no. If you mean accessing data from other devices, then you need to upload the data to the cloud (iCloud, or Parse.com, for instance). I don't really understands your needs here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.