Dear cocoa-dev,

Running on iPad simulator 6.1, I have a hazardous bug when using core data. The 
bug occurs 1 time over 5 approximately when I try to fetch data. The problem 
comes from my configuration of the core data stack with nested contexts. 
Indeed, when I remove the parent context and work directly with only one 
context (with NSMainQueueConcurrencyType type), I don't have bugs anymore.

Here is the code for the buggy configuration.

If you have any advice (including links), thank you !

- (void)initTheCoreDataStack
{
    NSManagedObjectModel * model = [NSManagedObjectModel 
mergedModelFromBundles:nil] ;
    NSPersistentStoreCoordinator * coordinator = [[NSPersistentStoreCoordinator 
alloc] initWithManagedObjectModel:model] ;
    
    NSDictionary *optionsForMigration = @{
                                          
NSMigratePersistentStoresAutomaticallyOption: @(YES),
                                          
NSInferMappingModelAutomaticallyOption : @(YES),
                                          NSSQLitePragmasOption: 
@{@"journal_mode":@"DELETE"}
                                          } ;
    
    
    /*
     Static data
     */
    NSURL * URLStaticData = [[NSBundle mainBundle] 
URLForResource:kNameStaticDataFile
                                                    
withExtension:kNameDataExtension] ;
    NSMutableDictionary *options = [optionsForMigration mutableCopy] ;

    //FIXME : problem -> migrating + read-only seems to not work
    //[options addEntriesFromDictionary:@{NSReadOnlyPersistentStoreOption : 
@(YES)}] ;
    
    NSError * error ;
    [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:@"ConfigurationForMyApp"
                                        URL:URLStaticData
                                    options:options
                                      error:&error] ;
    
    /*
     Dynamic data
     */
    NSString * nameDynamicFile = [NSString stringWithFormat:@"%@.%@", 
kNameDynamicDataFile, kNameDataExtension] ;
    NSURL * URLDynamicData = [[[self class] documentsFolderURL] 
URLByAppendingPathComponent:nameDynamicFile] ;
    
    [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:@"ConfigurationForAppData"
                                        URL:URLDynamicData
                                    options:optionsForMigration
                                      error:&error] ;
    
    
    /*
     Creation of the MOC
     */
    NSManagedObjectContext * theParentPrivateMOC = 
    [[NSManagedObjectContext alloc] 
initWithConcurrencyType:NSPrivateQueueConcurrencyType] ;
    [theParentPrivateMOC performBlock:^{
        theParentPrivateMOC.persistentStoreCoordinator = coordinator ;
    }] ;
    
    NSManagedObjectContext * theMainMOC = [[NSManagedObjectContext alloc] 
initWithConcurrencyType:NSMainQueueConcurrencyType] ;
    theMainMOC.parentContext = theParentPrivateMOC ;
    
    
    /*
     Instanciation of the MOC
     */
    _managedObjectContext = theMainMOC ;
}


Thanks !

Colas
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to