I need to improve the time of one fetch request to coredata, so I was thinking in indexing some attributes.
My doubt is, I will insert the rows manually in the sqlite file with sql, so will the indexing of the attributes have any effect? Or I need to insert the data by code?
This is a Bad Idea. The database isn't yours to insert data into. The Core Data framework does not expect its data to change underneath it, and you will almost certainly get unusual/unexpected results.
If Core Data doesn't perform well enough for you (and it may not, as described here), then you should consider just using SQLite directly, and managing your own database.
Related
I am creating an app that will show users what the statuses of some devices are. The status can change at any time and the users will be able to refresh the data. I want to use a way of storing the data so users are always able to see the latest state without needing an internet connection and to reduce the loading time.
I am currently deciding between Core data and SQLite.
For my situation, which one should I choose and why?
First of all Core data is not a database. So if you have feeling of database usage in future than move to SQLite.
Core data is a framework that manages object graph. Core data persist that object graph by writing it to disk.
Where as SQLite is a lighter version of SQL, So it limits some complex functionality of SQL language, And yes it is a pure database.
Core data is not a database how ever you can manage object and entities as like database tables and attributes as a columns.
Conclusion:
Go for Core data, it will reduce the time of database connection opening and querying. As I said it is not a database so all database limitations are not in a picture.
Core Data is the best option to use but if somehow you want to port the application to android or windows and want to keep the code similar then you can go for SQLITE as SQLite is supported by all major platforms. Whereas core data is only part of iOS
Using CoreData is always a better option.
Core Data is built on top of SQLite, so you work on SQLite either way :)
Core Data works with Objects so no worries working with Queries.
Complex queries and data retrieval is easier.
Database locks and other sync errors are auto handled.
Finally, it has everything that you require at the moment.
What the best way and how to work with multiples tables in iOS project and does FMDB supports relationships between tables?
Should I use Coredata or the FMDB wrapper?
Whatever you want to do, you can use either Core Data or FMDB + SQLite.
Basically, if one of the follow is true, I suggest using SQLite.
Performance is really important
You already have a huge SQLite database from elsewhere
You plan on making the database cross-platform somehow
Have a look at this blog post to read about the difference between SQLite and Core Data.
However, in most cases Core Data is a better way to go. Core Data is a great framework that helps you keep your consistency (using object graphs) and is really quite easy to use. It has received a bad rep for its performance, but it is actually not that bad. For instance, over-fetching is a common thing that is used to improve performance. This means fetching lots of data and then filtering out the data you actually want. This works great in iOS devices since their RAM is actually really good these days. Use it!
Also, if you plan on using Core Data, you should understand that it is NOT a wrapper for SQLite. You should not be thinking about tables like you normally would. In Core Data you have an object graph with entities instead of tables.
Conclusion:
Use Core Data unless you already have a SQLite database form elsewhere OR performance matters a lot (and I mean a lot).
There is no one good question for this. It's depends on project.
In my opinion if you have already some data which you want to import to the application, and you will facing huge amount of data in your database, you should go for pure sqlite (and maybe FMDB).
If you are connecting to REST, want to use TableViews, starting from empty database CoreData will right answer for you.
FMDB is only a wrapper on sqlite so it will have relations, because sqlite has.
I want to leave the Core Data to use the FMDB, but i want know some information if possible, in core data to reload a table view there is the NSFetchedController, that doesn't reload the data every time, but reload it when it necessary, so the reload of a UITableView is very fast (or i think do this maybe i wrong) in my project i have a UITableView that load the row from a query in the DB, so in the ViewWillAppear every time i call the method that query the DB, insert the information in a Array and reload the table view, but when the row in table view increase, the view takes time to appear, instead in Core Data the view display faster, so my question is, there is a way to for example cache data? to check if something change and need to make the query again?
Another question, to make the query faster i have add some index, i have to create an index also for the primary key?
I would suggest you first get familiar with some elementary concepts:
"Core Data in FMDB" is impossible. FMDB is a wrapper around the SQLite api, not around Core Data. Core Data is an object graph, not a database.
If you want to use Core Data, throw out FMDB (which I recommend).
In Core Data the fetched results controller uses a cache mechanism that makes tables very fast and efficient with resources.
In Core Data's managed object model editor, you can easily mark attributes to indexed for faster fetches.
Core Data has many more mechanisms to boost performance, such as fetch batch sizes, faulting and fetch request templates.
Core Data does not use foreign keys, but so-called relationships. The foreign-key concept is completely hidden and just an implementation detail. You should understand the difference between a relational database (SQLite) and an object graph (Core Data).
Read all about it here.
I haven't found an explicit "no" in the documentation/discussion but suspect it is not possible to generate CoreData objects programmatically, at runtime.
What I want to do is similar to executing DDL commands (eg. "Create Table", "Drop Table" etc.) from inside running code, because I don't know until I ask the user how many columns his table needs, or what data types they need to be. Maybe he needs multiple tables, at that.
Does anyone know whether this is possible? Would appreciate a pointer to something to read.
(Would also appreciate learning the negative, so I can stop wondering.)
If not doable in CoreData, would this be a reason to switch to SQLite?
You can create the entire Core Data model at run time-- there's no requirement to use Xcode's data modeler at all, and there's API support for creating and configuring every detail of the model. But it's probably not as flexible as it sounds like you want it to be. Although you can create new entity descriptions or modify existing ones, you can only do so before loading a data store file. Once you're reading and writing data, you must consider the data model as being fixed. Changing it at that point will generate an exception.
It's not quite the same as typical SQLite usage. It's sort of like the SQLite tables are defined in one fie and the data is stored in another file-- and you can modify the tables on the fly but only before loading the actual data. (I know that's not how SQLite really works, but that's basically the approach that Core Data enforces).
If you expect to need to modify your model / schema as you describe, you'll probably be better off going with direct SQLite access. There are a couple of Objective-C SQLite wrappers that allow an ObjC-style approach while still supporting SQLite-style access:
PLDatabase
FMDB
I am new to Core Data and i got stuck with a problem.
I have a file "db.sqlite" which has 3 tables and each containing around 10k records.
Now my question is can I add this file to my project and access(Select/Update) the data using CoreData API.
Thank You in advance.
You can't use Core Data to read an arbitrary SQL file. Neither can you reliable convert an arbitrary SQL file into an SQL file that Core Data can use. The sqlite schema for Core Data is undocumented and ever changing. Core Data will only read and write its schema and each each sqlite store file must be configured to a specific data model file. Direct creation of a SQL store for Core Data is always a fragile solution.
It is important to understand that Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.
As Mundi pointed out, you need to read the existing SQL file in using the standard SQL C API and then load that data in managed objects and have Core Data save the objects into its SQL store. It might sound like a lot of extra work but in reality you are translating from the limited SQL API which is concerned solely with getting data on and off disk to the Core Data API which provides a complete data model for your app.
The Core Data API actually hides the mechanics of the so-called persistent store. It is more of an object modeling API that can persist data very efficiently. It could also use a database different from sqlite.
Thus, if you want to use Core Data, I would recommend first importing all the records and creating a new database file with Core Data. You can use the SQLite API to read the existing database and the Core Data API to write out the new object oriented data into the new database.
Once all of your original data is in Core Data, you should stick with that.