1

hi i am writng an iphone app and need a bit of help.. i have this uitableview which displays data that is contained in a nsmutable array.. this data is retrieved via some operations therefore amount of data is not the same every time...

what i want to know is if i had something like:

NSMutableArray *mArray;

how would i go about storing and loading the 'mArray' from an sqlite database..

Further info as requested:

@Caleb

@westsider

Array Contents: The contents of the array are simple module names (i.e subjects that a student learns on a course), that are retrieved from a .ics calendar file with some parsing operations.. i end up with an array that contains the data i want and then i display it using a tableview..

Basically i need to initially save the module names that are stored in the array so that the next time the user opens the application, the module names are still there..

Why Core Data When the user selects a cell in tableview (i.e. selects a module) i want to push another view controller that displays the module name where the user can add other data/strings that should be saved..

Ibz
  • 518
  • 1
  • 8
  • 26
  • 1
    Check out Archiving and serialization http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i . See if that may help, more than likely you are going to need to store the contents as data in NSUserDefaults or to the file system to make things easier. – Joe Mar 22 '11 at 13:18
  • how would i use NSUserDefaults? – Ibz Mar 22 '11 at 13:19
  • I changed the link in my comment above to give an overview of archiving and serialization. Here is a linke to `NSUserDefault`s http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html There are array methods to store the values. – Joe Mar 22 '11 at 13:22

2 Answers2

2

What are the objects in the array? Are they all the same type? If they're dictionaries, do they all have the same keys? Are they made up of standard property list objects (NSDictionary, NSData, NSArray, NSString, NSNumber)? Why do you want to use an SQLite database rather than saving them some other way?

If you have an array of custom objects, the simplest thing to do is to implement NSCoding in the class (or classes) used in the array. Then, create an NSKeyedArchiver and archive your array using -archiveRootObject:toFile:.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    +1 for asking all the questions that I would ask ;) The two big ones being: what do your objects look like? and why core data (are you going to be reusing stored data or refetching each time app is run)? – westsider Mar 22 '11 at 17:13
  • @westsider Sorry for the really late reply.. I have updated my question if you could please scroll up to it as it was a bit too big for a comment.. Thank you for your replies :) – Ibz Mar 23 '11 at 05:39
  • Having read your updated question, I'd point out that your array of modules could still be a fairly straightforward array of dictionaries that could be saved either by archiving or even serialized to a property list. I like Core Data a lot, but it may be more than you need or are ready for. – Caleb Mar 23 '11 at 05:50
1

As long as everything in your array conforms to NSCoding you can serialize your array. Look at Archives and Serialization Programming Guide for creating and decoding archives.

You will need a column in your sqlite table that is of data type blob and use sqlite3_bind_blob to store it.

This link shows how to create an archive from an object. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Articles/creating.html#//apple_ref/doc/uid/20000949-BABGBHCA

This post shows how to retrieve a blob from a sqlite database. retrive-nsdata-from-sqlite-stored-in-blob-data-type-for-iphone-app

Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135