web-dev-qa-db-fra.com

Données de base: erreur: une exception a été interceptée lors du traitement de la modification des données de base

Cela fait quelques jours que je suis confronté à ce problème et c'est vraiment frustrant. J'ai révisé mon code maintes et maintes fois et j'ai essayé la même chose et je continue à avoir le même problème. Ce qui n'arrive que 50% des fois, mais pas toujours. Cela rend plus difficile ..

Le problème,

Je suis en train d'analyser les données de 3 fichiers csv dans mon Core Data, l'analyse de 2 des fichiers se passe toujours bien, mais le fichier milieu/second correspond toujours au crash, il sera alors l'adresse de ce fichier et de la classe managedObjectContext pour ce fichier. .

Message d'erreur

CoreData: error: Serious application error.  
Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  
-[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)
2014-09-12 11:27:06.115 AppName[210:3907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'

Ainsi, dans mon FetchData class, j'essaie de résoudre le problème de différentes manières.

  • Tout d'abord, j'ai changé mon fichier .csv et inséré N/A dans tous les champs/cellules vides.
  • Deuxièmement, je vérifie ma FetchData si cela n’a aucune valeur, enregistrez N/A.
  • Troisièmement, dans mon contrôleur de vue où je lance l'analyse des données, j'ai maintenant séparé trois propriétés différentes pour ces 3 entités dans mes données de base.

@property (nonatomic, strong) NSManagedObjectContext * managedObjectContext;

@property (nonatomic, strong) NSManagedObjectContext * managedObjectContextGI;

@property (nonatomic, strong) NSManagedObjectContext * managedObjectContextVA;

C'est peut-être un peu fou ou autre chose, mais j'ai vraiment besoin de résoudre ce problème, essayer toutes les solutions ou approches possibles est toujours bon, je pense. 

ViewController à appeler les fonctions pour effectuer l'analyse.

//at the beginning of my model
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

-(IBAction)myLoadingTask:(id)sender{

       dispatch_async(kBgQueue, ^{

           NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

           NSString *savedValue = @"";

           if([[userDefaults stringForKey:@"dataFetched"] length] > 0){                            
               savedValue = [userDefaults stringForKey:@"dataFetched"];
           }

           // if the csv files data hasn't been fetch it, then fetch it
           if([savedValue length] == 0){

               FetchData *fd = [[FetchData alloc] initWithManagedContext:self.managedObjectContext];

               // fetching benefits data
               [fd beginParser];

               FetchGIBillData *fdGI = [[FetchGIBillData alloc] initWithManagedContext:self.managedObjectContextGI];

               // fetching gi bill data
               [fdGI beginParser];

               FetchVAPhones *fdVA = [[FetchVAPhones alloc] initWithManagedContext:self.managedObjectContextVA];

               // fetching va phones
               [fdVA beginParser];

               NSString *valueToSave = @"saved";
               [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"dataFetched"];
               [[NSUserDefaults standardUserDefaults] synchronize];

           }

       });
}

Ceci est mon Core Data fonctions de modèle et ainsi de suite .. Ce que j’ai fait vérifier si elle était vide, sauvegardez N/A et ainsi de suite .. toutes mes propriétés qui se trouvent dans mon entité sont cordes

#define GIBILL_FILENAME @"gi_bill_data"

int numOfEntries;
- (id)initWithManagedContext:(NSManagedObjectContext*)managedObjectContext
{
    self.managedObjectContext = managedObjectContext;
    arrayOfRecords = [[NSMutableArray alloc] init];
    numOfEntries=0;
    return self;
}


- (void) beginParser
{
    if (self.managedObjectContext == nil){
        // Error: Must pass in NSManagedObjectContext
        return;
    }

    NSString *filePath = [[NSBundle mainBundle] pathForResource:GIBILL_FILENAME ofType:@"csv"];
    NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
    NSStringEncoding encoding = NSUTF8StringEncoding;//NSWindowsCP1250StringEncoding;

    CHCSVParser *parser = [[CHCSVParser alloc] initWithInputStream:stream usedEncoding:&encoding delimiter:','];
    parser.delegate = self;

    [parser parse];

    // uncomment to update data x amount of dates
    //[self checkDateForRefreshCSV:parser];

}

** C'est là que se passe la sauvegarde !! *

#pragma mark - Data Add
/**
 * addRows
 * @param parser: the CHCSV parser that will parse if required refresh
 * @brief: add the row to ths managedObjectContent DB. All values saved.
 */
- (void) addRows:(CHCSVParser *)parser
{
    int i = -1;
    if ([arrayOfRecords count] == 0) return;
    GIBill *data = [NSEntityDescription
                      insertNewObjectForEntityForName:@"GIBill"
                      inManagedObjectContext:self.managedObjectContext];

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.facility_code = [arrayOfRecords objectAtIndex:i];
    else
        data.facility_code = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.institution = [arrayOfRecords objectAtIndex:i];
    else
        data.institution = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.city = [arrayOfRecords objectAtIndex:i];
    else
        data.city = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.state = [arrayOfRecords objectAtIndex:i];
    else
        data.state = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.country = [arrayOfRecords objectAtIndex:i];
    else
        data.country = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.bah = [arrayOfRecords objectAtIndex:i];
    else
        data.bah = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.poe = [arrayOfRecords objectAtIndex:i];
    else
        data.poe = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.yr = [arrayOfRecords objectAtIndex:i];
    else
        data.yr = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.gibill = [arrayOfRecords objectAtIndex:i];
    else
        data.gibill = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 0)
        data.cross = [arrayOfRecords objectAtIndex:i];
    else
        data.cross = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.grad_rate = [arrayOfRecords objectAtIndex:i];
    else
        data.grad_rate = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.grad_rate_rank = [arrayOfRecords objectAtIndex:i];
    else
        data.grad_rate_rank = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.default_rate = [arrayOfRecords objectAtIndex:i];
    else
        data.default_rate = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.avg_stu_loan_debt = [arrayOfRecords objectAtIndex:i];
    else
        data.avg_stu_loan_debt = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.avg_stu_loan_debt_rank = [arrayOfRecords objectAtIndex:i];
    else
        data.avg_stu_loan_debt_rank = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.indicator_group = [arrayOfRecords objectAtIndex:i];
    else
        data.indicator_group = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.salary = [arrayOfRecords objectAtIndex:i];
    else
        data.salary = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.Zip = [arrayOfRecords objectAtIndex:i];
    else
        data.Zip = @"N/A";

    if([[arrayOfRecords objectAtIndex:++i] length] > 2)
        data.ope = [arrayOfRecords objectAtIndex:i];
    else
        data.ope = @"N/A";

    NSError *error;

    [self.managedObjectContext save:&error];

}

Eh bien, j'ai posté le code le plus pertinent que je pense à la question. S'il vous plaît, si quelque chose d'autre est nécessaire ou plus de détails sur le problème, faites le moi savoir et je le fournirai. 

Merci d'avance!

22
NorthBlast

Eh bien, tout le problème était de créer la NSManagedObjectContext et tout ce qui se trouvait dans le Main Thread, puis d’y accéder ou de l’utiliser dans le Background Thread

Donc, je viens de suivre ce post et maintenant tout fonctionne parfaitement et sans heurts :) 

Merci beaucoup pour les commentaires, cela m’a vraiment mis dans la bonne direction et c’était tout ce dont j'avais besoin pour pouvoir trouver le problème. 

Merci!

Dans le AppDelegate.h 

+ (NSManagedObjectContext *)mainQueueContext;
+ (NSManagedObjectContext *)privateQueueContext;

Puis, dans mon AppDelegate.m

#pragma mark - Singleton Access

+ (NSManagedObjectContext *)mainQueueContext
{
    return [self mainQueueContext];
}

+ (NSManagedObjectContext *)privateQueueContext
{
    return [self privateQueueContext];
}

#pragma mark - Getters

- (NSManagedObjectContext *)mainQueueContext
{
    if (!_mainQueueContext) {
        _mainQueueContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        _mainQueueContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
    }

    return _mainQueueContext;
}

- (NSManagedObjectContext *)privateQueueContext
{
    if (!_privateQueueContext) {
        _privateQueueContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
        _privateQueueContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
    }

    return _privateQueueContext;
}

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(contextDidSavePrivateQueueContext:)
                                                     name:NSManagedObjectContextDidSaveNotification
                                                   object:[self privateQueueContext]];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(contextDidSaveMainQueueContext:)
                                                     name:NSManagedObjectContextDidSaveNotification
                                                   object:[self mainQueueContext]];
    }
    return self;
}

#pragma mark - Notifications

- (void)contextDidSavePrivateQueueContext:(NSNotification *)notification
{
    @synchronized(self) {
        [self.mainQueueContext performBlock:^{
            [self.mainQueueContext mergeChangesFromContextDidSaveNotification:notification];
        }];
    }
}

- (void)contextDidSaveMainQueueContext:(NSNotification *)notification
{
    @synchronized(self) {
        [self.privateQueueContext performBlock:^{
            [self.privateQueueContext mergeChangesFromContextDidSaveNotification:notification];
        }];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Et enfin dans mon ViewController où j'envoie le travail en arrière-plan ..

// dont forget the macro
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

dispatch_async(kBgQueue, ^{

    id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate privateQueueContext];

    // do something in the background with your managedObjectContext!!!!

 });
22
NorthBlast

La ligne ci-dessous a sauvé ma journée:

_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

Il vous suffit de définir le type de concurrence comme privé. De cette façon, il peut effectuer plusieurs opérations sur la base de données simultanément sur les files d'attente privées.

27
Mohit Manhas

Dans Swift 4, j'utilise ce modèle:

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
    /*
     The persistent container for the application. This implementation
     creates and returns a container, having loaded the store for the
     application to it. This property is optional since there are legitimate
     error conditions that could cause the creation of the store to fail.
     */
    let container = NSPersistentContainer(name: "Model")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

lazy var viewContext: NSManagedObjectContext = {
    return self.persistentContainer.viewContext
}()

lazy var cacheContext: NSManagedObjectContext = {
    return self.persistentContainer.newBackgroundContext()
}()

lazy var updateContext: NSManagedObjectContext = {
    let _updateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
    _updateContext.parent = self.viewContext
    return _updateContext
}()

et updateContext pour les opérations de mise à jour.

0
Anton Sokolov