Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Core Data useful for readonly data too?

I'm creating an iPhone App and am wondering whether Core Data is better for readonly data than a SQLite database. It feels like the SQLite DB is the better choice, is that right? Can I even pre-fill the Core Data storage?

Basically, I need like 3 tables with a bunch (up to 3000) of entities each. I then want to list the data in TableViews, search on it or load things for other purposes.

Should or can I use Core Data in this case?

like image 503
Christian Avatar asked Oct 28 '09 16:10

Christian


People also ask

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What kind of database is Core Data?

Core Data is not a database. Core Data is a framework for managing an object graph. An object graph is nothing more than a collection of interconnected objects. The framework excels at managing complex object graphs.

Where is Core Data stored?

The persistent store should be located in the AppData > Library > Application Support directory.


2 Answers

If you will be displaying your read-only data in a table view, there can be significant advantages to using Core Data over SQLite simply due to NSFetchedResultsController. This convenience class makes it extremely easy to display database elements in a table view, and it can handle batched fetching for you. The batched fetching lets you load only the information you need on screen right then, dramatically improving loading time and memory usage for all but the smallest data sets.

I use Core Data for read-only information that I ship in my application's bundle for this reason, along with the fact that I can share a data model with the writable database stored in the user's application data. My recommendation is to go with Core Data unless you absolutely need to target iPhone OS 2.x devices.

like image 67
Brad Larson Avatar answered Nov 16 '22 22:11

Brad Larson


Here's a simple way to preload the Core Data store using plists.

Make a property list containing an array of dictionaries. Make the keys of each dictionary correspond to the keys of your managed object.

alt text

Then, call this method the first time the app launches:

- (void)loadDataFromPropertyList {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"someFile" ofType:@"plist"];
    NSArray *items = [NSArray arrayWithContentsOfFile:path];

    NSManagedObjectContext *ctx = self.managedObjectContext;

    for (NSDictionary *dict in items) {
        NSManagedObject *m = [NSEntityDescription insertNewObjectForEntityForName:@"TheNameOfYourEntity" inManagedObjectContext:ctx];
        [m setValuesForKeysWithDictionary:dict];
    }

    NSError *err = nil;
    [ctx save:&err];

    if (err != nil) {
        NSLog(@"error saving managed object context: %@", err);
    }
}

Call loadDataFromPropertyList the first time the app launches by including the following code in the implementation of application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
if (![defaults objectForKey:@"firstRun"])
{
    [defaults setObject:[NSDate date] forKey:@"firstRun"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self loadDataFromPropertyList];
}
like image 33
Rose Perrone Avatar answered Nov 17 '22 00:11

Rose Perrone