• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Advice for an hazardous bug with nested contexts in core data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Advice for an hazardous bug with nested contexts in core data


  • Subject: Advice for an hazardous bug with nested contexts in core data
  • From: Colas B <email@hidden>
  • Date: Wed, 09 Jul 2014 09:59:43 +0100

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 (email@hidden)

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:

This email sent to email@hidden


  • Prev by Date: NSTableView Selection highlight issue in 10.10
  • Next by Date: Re: Considering some of the questions recently…
  • Previous by thread: Re: NSTableView Selection highlight issue in 10.10
  • Next by thread: IOS Navigator strategies.
  • Index(es):
    • Date
    • Thread