Questions about CoreData and pre-populated models

To pre-populate CoreData in my app upon first launch, I have included a PreModel.sqlite file that was previously created by the app from data that it downloaded from a web service, which includes images.

To populate the model, I do this:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{    
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSLog(@"creating new store");

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"PreModel.sqlite"];

    if(![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {

        NSString *sqlitePath = [[NSBundle mainBundle] pathForResource:@"PreModel" ofType:@"sqlite"];
        if (sqlitePath) {
            NSError *error = nil;
            [[NSFileManager defaultManager] copyItemAtPath:sqlitePath toPath:[storeURL path] error:&error];
            if (error) {
                NSLog(@"Error copying sqlite database: %@", [error localizedDescription]);
            }
        }
    }     

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }   

    return _persistentStoreCoordinator;
}

It seems to work. But I have 2 questions:

  1. If the sqlite file is just a database file and does not actually contain any images, how is the app finding and loading the images on first launch?
  2. Even on subsequent runs of the app I see "creating new store" logged every time. Why is _persistentStoreCoordinator always nil? I am clearly setting it in the code.

Answers


  1. It's possible to store images in a database file, usually as binary blobs (which look like instances of NSData in Core Data). If you can provide more info about your model or the code that stores/loads the images, we can be more specific.
  2. "Creating new store" is expected to get logged every time the app is launched in this instance. Even though the SQLite file is persistent on disk, you can't expect data structures in your code to stick around when your app is terminated - you need to create a new persistent store object for your program every time it launches.

    Think of it like assigning NSInteger x = 10, then expecting to be able to quit and relaunch your program while maintaining that x has the value 10. That's not how programs work - you'd need to reassign x = 10 before you can expect to read x and get 10 back. The variable _persistentStoreCoordinator works the same way here.


Need Your Help

A twitter javascript library that shows retweets?

javascript jquery twitter

So far I was using twitterjs, but it doesn't show retweets from my timeline. So I'm just looking for an alternative able to show retweets.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.