mardi 3 novembre 2015

NSPersistentStoreCoordinator raises error NSSQLiteErrorDomain=26 when trying to access sqlite file

Recently I added some concurrent accesses to my different domain models (which represent a subset of the persisted data). My crash reporting shows that I now stumbled upon some errors on iOS7,iOS8 & iOS9, that all seem to be based on

NSSQLiteErrorDomain=26, NSUnderlyingException=File at path does not appear to be a SQLite database at the position I marked in the source code

Unfortunately I can't reproduce it and I can't really imagine that the sqlite file doesn't exist on some devices, since I ship it with my application bundle.

  1. Is there a flaw in my code that prevents me from accessing the sqlite file?
  2. Is there another possibility this error can occur from except a missing file?

Heres' my source code for the initialization of my model singleton and the NSManagedObjectContext

+ (id) sharedModel {
    static id sharedModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^ {
        sharedModel = [[self alloc] initWithStoreURL: kDefaultStoreURL];
    });
    return sharedModel;
}

- (id) initWithStoreURL:(NSURL *)storeURL {
    self = [super init];
    if (self) {
        // create managed object model
        NSURL * modelURL = [[NSBundle mainBundle] URLForResource: @"MyApp" withExtension: @"momd"];
        NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL];
        // create persistent store coordinator
        NSError * error = nil;
        NSPersistentStoreCoordinator * storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel];
        if (![storeCoordinator addPersistentStoreWithType: NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            CLS_LOG(@"Unresolved error %@, %@", error, [error userInfo]); 
            // this is where the error occurs
            abort();
            return;
        }
        // init managedObjectContext
        context = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSMainQueueConcurrencyType];
        [context setPersistentStoreCoordinator:storeCoordinator];
        _mainContext = context;

        _someDomainModel1 = [[SomeDomainModel1 alloc] initWithContext:_mainContext];
        _someDomainModel2 = [[SomeDomainModel2 alloc] initWithContext:_mainContext];
    }
    return self;
}

Aucun commentaire:

Enregistrer un commentaire