I'd want to make a program that could save different values and store it in a file and, then that the user can open this file every time he wants to. Need some help!
You are looking for persistence. There are some options available:
1) Using SQLite
2) Use Core Data
3) Write it as text to file
Core Data is recommended.
If you need complex persistence then you should use Core Data. If you are thinking about just saving a text typed by the user (as a NSString), use the following method:
writeToFile:atomically:encoding:error:
You can alternatively save a NSArray into a plist file using the following method:
writeToFile:atomically:
Related
I'm wrinting a very simple application which can read data from a specified file and populate the fetched data to a NSTableView. The file is a plain text file where each line represents an object (I need to parse the file). I want to use Core Data and my question is what is the Cocoa way to do that?
My first idea is to parse the file and create instances for the Entity which represents one line. I'm not sure that is it the best solution. And later I'll write out the changes to the file (after save? or automatically after a given amount of time?)
My configuration is Mountain Lion and the latest XCode.
A single entity with a number of attributes sounds good. If you had an attribute which holds a reasonable amount of data and was repeated on a number of 'rows' then it would be a candidate for another entity.
Yes, each instance of your entity would represent one row in your table.
Personally, I would either save on request by the user, or not have a save button and save each change. Your issue is the conversion between your in-memory store and on-disk store. Using a plain text file doesn't help (though, depending on the file format, it could be possible to edit individual lines in the file using NSFileHandle).
Generally it would still make more sense to use a better format like XML or JSON and then make use of a framework like RestKit which will do all of the parsing and mapping for you (after you specify the mapping configuration).
You can also use bindings to connect your data to your NSTableView. Another ref.
I want to create a large database. I am thinking of using core data for this purpose.But i want to insert the data to it manually and only once. This data is never deleted or edited but only read. How can i use core data to add such large number of objects to database? How to create database in core data and write only once.
Your question is quite general but I'll try to give you some hints on it.
Based on my experience, the simplest way to achieve it, it's to follow these two steps:
1) You could create an external file, in XML, JSON or plist format, that you can parse and use to create a prepulated a Core Data store. You can find some info in core-data-tutorial-how-to-preloadimport-existing-data-updated. In particular, you could set up a dummy project (or just use the AppDelegate methods) and use it to create the store, e.g. MyDataStore.sqlite.
2) Once you created, forget about the routines you have previously used and you ship the store in the application's bundle of your app. There the store is read-only. Otherwise, if you need to modify it, you are not allowed to do it and so you need to move it, for example, to the document directory.
You cand find additional info in the following SO topics:
Is Core Data useful for readonly data too?
How can I ship my app with a pre-populated Core Data database?
Core Data Store included in App Bundle
P.S. If you are in production and you need to make same change to your store, you need to republish the app (since the store is within the main bundle). To avoid this you need to move the store to a different directory and set up, for example, a sort of sync mechanism.
Hope that helps.
I am intending to use Core Data to store a static list of data which consist of objects with attributes (e.g. object = person, attributes = weight, height etc).
Any advise on what is the best way to approach this? What raw file format should I need to use and what is the best way to load the data from the raw file to core data?
Appreciate any advise and help on this
If the data set is large, you should include a prepopulated SQLite store in the app bundle.
Create a separate project in Xcode and import the data model and NSManagedObject subclass files (if any.) Write code to import or create the static data and write it all to the persistent store.
Copy/add the persistent store file to the release project. In the release project, set the store pathway to the location of the store file in the app bundle using [NSBundle pathForResourse:Type]. In the persistent store options, mark the store as readonly.
That will let you set up a prepopulated, readonly Core Data stack that will efficiently handle thousands of objects for you.
I'd suggest shipping binary .plist file with initial data and on first launch dump it to CoreData.
Also may I suggest you not use CoreData if your list is going to be immutable. Just read the plist on app startup and keep it in some AppDelegate's instance var.
Update (to reflect comment):
If it's a few thousand records - you SHOULD import it to core data as it would improve the performance of data retreval. Also - I would not suggest keeping ALL of them in the memory.
To get data into plist you could use some other programming language to select all the data from database and export it to xml plist (there are libraries for virtually any programming language). Then by using property list editor you would be able to export it to binary plist.
Then depending on what you need to do with your data - you can import it either in background or just show a progress indicator/bar to the user while using main thread for import. I believe plist would take few MB's. Also - try benchmarking a thousand records just to get approximate estimate of how long data import would take.
1, You could fill in core data on startup from external source. On startup, read data from external source(SQLite database or XML file), then insert the data into core data;
2, Provide pre-filled in SQLite database. For this we’ll let Core Data create the database structure for us based on the model, and then we populate the database with a utility app. The utility app could be a Mac or iPhone app that uses Core Data to populate the database via Core Data APIs, or some kind of program that fills in the SQLite database directly. Once the database is populated, just include it with the app and make the app use it as the default database if no database already exists.
Hope this tutorial will help.
in my iphone application i've a Xml file downloaded from a server which contains 20-30 strings. In the app i want to access particular nodes, the user insert two numbers and i want the corrispondent strings.
Is more performant access each time the XML file and scroll node until i reach the two indices entered by user or is it better convert the Xml file into a PLIST and then, import PLIST into an array and access data by it?
Every suggestion is welcome.
Depends on how often you intend to access it, really. An alternative to the plist is to simply parse the XML and store the data on the phone at startup or whenever the XML file is downloaded. That way even if your users access it repeatedly you'll only actually go through it one time.
I have a mosques database that has 1000 items ..
I want to use Core Data approach in accessing my database ..
I have already tried the SQLite approach to create the database
where I have a text Pad file with all data seperated by tabs
and then Import the data from txt file to sql file ..
Now this works fine ..
I want to know how can I import data from my SQL file to the newly created Core Data Project
Shall I add SQL file to resources ??
Copy it or not ??
I have looked at CoreDataBooks example but I think I'm missing something
I want to know the exact way for adding an SQL file to the resources of a Core Data Project ..
You can't.
You should regard the fact that Core Data uses SQLite as the format to save the file as an implementation detail, not to be used directly unless you really, really, really need to do that. For example, you can't expect Core Data to work alright if you also directly writes on to the SQLite file.
Instead, read the Core Data documentation, and import the data directly from the tab-separated text file to the Core Data context, and let the Core Data save it to the file. Yes it does use SQLite behind the scenes, but it's better for you to forget that fact.
Yuji and Dave DeLong are right on both accounts, however I feel like I should add that just because you can't realistically feed CoreData a pre-populated SQLite file doesn't mean you can't bootstrap your CoreData store from a SQLite file (or a text file or anything else.) It just means that you have to do the work yourself.
For instance, you could include your pre-populated SQLite file (with it's own, non-CoreData schema, etc) as a resource in the project. Then when your app starts up, if it sees that the CoreData store is empty, you can use the SQLite API directly to open/query your bootstrapping database and translate the results into operations that generate the desired object graph in CoreData. The next time the app starts up, the CoreData object graph will be populated, and you won't have to do it again.
The take away here is that while it's not "free," it's not "impossible." Many, many apps include built-in CoreData repositories that contain data. That data had to be bootstrapped from somewhere, right?