2015-03-14 2 views
3

Я использую CoreData в приложении iOS для управления словами в приложении «флеш-карты» для людей, изучающих новые языки.CoreData managedObjectContext save Нарушает UNIQUE ограничение для Z_PK

Проблема, с которой я столкнулась, заключается в том, что когда я настраиваю данные для нового объекта и пытаюсь сохранить его в хранилище данных, я получаю нарушение требования UNIQUE CONSTRAINT для базы данных sqlite. Этот столбец представляет собой столбец Z_PK, который, как я понимаю, является PRIMARY KEY, созданным основными методами данных iOS, когда хранилище данных изначально создано.

Вот UNIQUE CONSTRAINT сообщения я получаю, когда я пытаюсь сохранения:

2015-03-14 09:25:14.427 ghoti[25856:1107373] CoreData: error: (1555) UNIQUE constraint failed: ZGHOTIENTITY.Z_PK (lldb)

Z является префиксом застрял на все эти SQL столбцов
GHOTIENTITY моего магазин данных
Z_PK является основным ключ

Вот быстрый скриншот таблицы данных внутри SQLITE «редактор» в браузере Firefox: datastructure from sqlite

определение бизнес-объекта сущности само по себе не включает столбец Z_PK:

@implementation ghotiEntity 

@dynamic numberCorrect; 
@dynamic numberIncorrect; 
@dynamic likelihoodOfPresentingWord; 
@dynamic englishText; 
@dynamic hebrewText; 
@dynamic phoneticText; 

@end 

я бы раздражало - но совместимый - если бы это было так просто, как только установка ghotiEntity.Z_PK до максимального значения Z_PK в хранилище + 1, но этот столбец не является даже частью определения сущности в первую очередь.


EDIT - Полезный пользователь попросил код, поэтому я добавляю его здесь, а не пытаться запихнуть его в комментарий:

Я использую коллекцию mmBusinessObject методов данных ядра, что Я видел, как плавает по сети. Кевин МакНиш объясняет это полностью here.

Основная структура такова:

mmBusinessObject.m и .h сделать хорошую работу оберточной методы managedObject во что-то «легко читать и иметь дело с». Для удобства чтения я скопировал фрагмент из двух элементов [entity createEntities] и [entity saveEntities] здесь и полный .m-файл в конце этого сообщения. (Обратите внимание, что я до сих пор не построили во всех проверки ошибок, я просто пытаюсь сделать основную работу функции

создать новый орган:.

// Creates a new entity of the default type and adds it to the managed object context 
- (NSManagedObject *)createEntity 
{ 
    return [NSEntityDescription insertNewObjectForEntityForName:self.entityClassName inManagedObjectContext:[self managedObjectContext]]; 
} 

СОХРАНИТЬ ИЗМЕНЕНИЯ

- (void)saveEntities 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() 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. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

Так что мой рабочий код (в моем файле ViewController.m) делает это: (обратите внимание, что ghotiEntity мой конкретный бизнес-объект

- (IBAction)saveButtonAction:(UIButton *)sender { 

    ghotiEntity *newGhotiEntry = (ghotiEntity *)[ghotiData createEntity]; 
    newGhotiEntry.englishText=self.englishWord.text; 
    newGhotiEntry.hebrewText=self.hebrewWord.text; 
    newGhotiEntry.phoneticText=self.phoneticWord.text; 
    newGhotiEntry.numberCorrect=0; 
    newGhotiEntry.numberIncorrect=0; 
    newGhotiEntry.likelihoodOfPresentingWord=0; 

    [ghotiData saveEntities]; 

    [self enableRegularUI]; 
    [self pickNextCard]; 
} 
.

Это из строки NSLog в методе saveEntities выше, что я получаю диагностическую информацию, что это столбец Z_PK, который имеет проблему UNIQUE CONSTRAINT.


Как и было выше, вот полный файл .m.Мне нужно убедиться, что ясно, что кредит для этого кода принадлежит либо Кевину МакНишу, как указано выше, либо любому другому сотруднику, который разместил тот же код в других местах ... просто выполните поиск в mmBusinessObject в google. Я думаю, что Кевин - создатель, и его учебник действительно хорош. Первые несколько строк в версиях, которые я видел, имеют имя Кевина в них!

#import "mmBusinessObject.h" 

@implementation mmBusinessObject 

// Initialization 
- (id)init 
{ 
    if ((self = [super init])) { 
     _copyDatabaseIfNotPresent = YES; 
    } 
    return self; 
} 

// Creates a new entity of the default type and adds it to the managed object context 
- (NSManagedObject *)createEntity 
{ 
    return [NSEntityDescription insertNewObjectForEntityForName:self.entityClassName inManagedObjectContext:[self managedObjectContext]]; 
} 

// Delete the specified entity 
- (void) deleteEntity:(NSManagedObject *)entity { 
    [self.managedObjectContext deleteObject:entity]; 
} 

// Gets entities for the specified request 
- (NSMutableArray *)getEntities: (NSString *)entityName sortedBy:(NSSortDescriptor *)sortDescriptor matchingPredicate:(NSPredicate *)predicate 
{ 
    NSError *error = nil; 

    // Create the request object 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

    // Set the entity type to be fetched 
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]]; 
    [request setEntity:entity]; 

    // Set the predicate if specified 
    if (predicate) { 
     [request setPredicate:predicate]; 
    } 

    // Set the sort descriptor if specified 
    if (sortDescriptor) { 
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
     [request setSortDescriptors:sortDescriptors]; 
    } 

    // Execute the fetch 
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 

    if (mutableFetchResults == nil) { 

     // Handle the error. 
    } 

    return mutableFetchResults; 
} 

// Gets all entities of the default type 
- (NSMutableArray *)getAllEntities 
{ 
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:nil]; 
} 

// Gets entities of the default type matching the predicate 
- (NSMutableArray *)getEntitiesMatchingPredicate: (NSPredicate *)predicate 
{ 
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:predicate]; 
} 

// Gets entities of the default type matching the predicate string 
- (NSMutableArray *)getEntitiesMatchingPredicateString: (NSString *)predicateString, ...; 
{ 
    va_list variadicArguments; 
    va_start(variadicArguments, predicateString); 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString 
                arguments:variadicArguments]; 
    va_end(variadicArguments); 
    return [self getEntities:self.entityClassName sortedBy:nil matchingPredicate:predicate]; 
} 

// Get entities of the default type sorted by descriptor matching the predicate 
- (NSMutableArray *)getEntitiesSortedBy: (NSSortDescriptor *) sortDescriptor 
         matchingPredicate:(NSPredicate *)predicate 
{ 
    return [self getEntities:self.entityClassName sortedBy:sortDescriptor matchingPredicate:predicate]; 
} 

// Gets entities of the specified type sorted by descriptor, and matching the predicate string 
- (NSMutableArray *)getEntities: (NSString *)entityName 
         sortedBy:(NSSortDescriptor *)sortDescriptor 
     matchingPredicateString:(NSString *)predicateString, ...; 
{ 
    va_list variadicArguments; 
    va_start(variadicArguments, predicateString); 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString 
                arguments:variadicArguments]; 
    va_end(variadicArguments); 
    return [self getEntities:entityName sortedBy:sortDescriptor matchingPredicate:predicate]; 
} 

- (void) registerRelatedObject:(mmBusinessObject *)controllerObject 
{ 
    controllerObject.managedObjectContext = self.managedObjectContext; 
} 

// Saves all changes (insert, update, delete) of entities 
- (void)saveEntities 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { 
      // Replace this implementation with code to handle the error appropriately. 
      // abort() 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. 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

#pragma mark - 
#pragma mark Core Data stack 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *)managedObjectContext { 

    if (_managedObjectContext != nil) { 
     return _managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) { 
     _managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [_managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return _managedObjectContext; 
} 

/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created from the application's model. 
*/ 
- (NSManagedObjectModel *)managedObjectModel { 

    if (_managedObjectModel != nil) { 
     return _managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:self.dbName withExtension:@"momd"]; 
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    return _managedObjectModel; 
} 

/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    // If the sqlite database doesn't already exist, create it 
    // by copying the sqlite database included in this project 

    if (self.copyDatabaseIfNotPresent) { 

     // Get the documents directory 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
     NSString *docsDir = paths[0]; 

     // Append the name of the database to get the full path 
     NSString *dbcPath = [docsDir stringByAppendingPathComponent: 
          [self.dbName stringByAppendingString:@".sqlite"]]; 

     // Create database if it doesn't already exist 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     if (![fileManager fileExistsAtPath:dbcPath]) { 
      NSString *defaultStorePath = [[NSBundle mainBundle] 
              pathForResource:self.dbName ofType:@"sqlite"]; 
      if (defaultStorePath) { 
       [fileManager copyItemAtPath:defaultStorePath toPath:dbcPath error:NULL]; 
      } 
     } 



    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent: 
         [NSString stringWithFormat:@"%@%@", self.dbName, @".sqlite"]]; 

    NSError *error = nil; 
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() 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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible; 
     * The schema for the persistent store is incompatible with current managed object model. 
     Check the error message to determine what the actual problem was. 


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     * Simply deleting the existing store: 
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

     */ 
     NSLog(@"%@",storeURL); 
     if ([error code] == 134100) { 
      [self performAutomaticLightweightMigration]; 
     } 
     else 
     { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
    return _persistentStoreCoordinator; 
} 

- (void)performAutomaticLightweightMigration { 

    NSError *error; 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", self.dbName, @".sqlite"]]; 

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                configuration:nil 
                  URL:storeURL 
                 options:options 
                  error:&error]){ 
     // Handle the error. 
    } 
} 


#pragma mark - 
#pragma mark Application's Documents directory 

/** 
Returns the URL to the application's Documents directory. 
*/ 
- (NSURL *)applicationDocumentsDirectory { 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 

@end 
+0

Просьба представить код, как вы создаете и сохраняете объекты ghoti. CoreData управляет закрытыми ключами внутри и обычно работает отлично, если все делать правильно. – DisableR

+0

благодарит за комментарий. Я отредактировал исходное сообщение, чтобы включить в него больше кода и контекста. – plm

+0

В реализации mmBusinessObject база данных копируется из проекта, если она существует. У вас есть предварительно сгенерированная база данных .sqlite в вашем проекте? – DisableR

ответ

1

Просмотрите свой код для всех мест, в которых вы создаете объект для БД. Убедитесь, что вы не создаете объекты из нескольких потоков.

+0

Выполнение одной и той же проблемы, но я не создаю объекты из нескольких потоков. – AndrewSmiley

0

я была такая же проблема, но я имел это, когда я хочу, чтобы обновить запись, поэтому я установил с refreshObject затем сохранить как это:

[dataManager.managedObjectContext refreshObject:object mergeChanges:YES]; 
[dataManager saveContextWithBlock:block]; 
1

У меня была такая же проблема с вопросом Z_PK ограничения. Это произошло из-за того, что я скопировал пустую базу данных sqlite из папки документов приложения и предварительно заполнил ее с помощью надстройки менеджера SQLite Firefox. Шаг, который я забыл, это обновление таблицы Z_PrimaryKey ... вам нужно обновить значение Z_MAX для каждой таблицы, чтобы Core Data знал следующий Z_PK для использования при добавлении новых записей в базу данных. Просто установите каждое значение Z_Max с количеством записей в каждой таблице. Сохраните его и верните в свое приложение, и все будет хорошо.

+0

Как вы это делаете, добавляя основные данные программно в db? – arniotaki

Смежные вопросы