Probleme beim Speichern von Objekten in Core Data

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Probleme beim Speichern von Objekten in Core Data

    ich komm einfach nciht weiter! Wo hab ich hier den fehler gemacht?

    Fehlermeldung beim Speichern eines Objekts in Core Data:

    2012-12-23 14:12:26.555 Reptiles0-0-3[35945:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Reptiles''
    *** First throw call stack:
    (0x1f9e012 0x13dbe7e 0x10e63e7 0x3b5b 0x37c4 0x13ef705 0x1a920 0x256b24 0x13ef705 0x1a920 0x1a8b8 0xdb671 0xdbbcf 0xdad38 0x4a33f 0x4a552 0x283aa 0x19cf8 0x1ef9df9 0x1ef9ad0 0x1f13bf5 0x1f13962 0x1f44bb6 0x1f43f44 0x1f43e1b 0x1ef87e3 0x1ef8668 0x1765c 0x243d 0x2365)
    libc++abi.dylib: terminate called throwing an exception


    Quellcode

    1. //
    2. // AddReptileViewController.m
    3. // Reptiles0-0-3
    4. //
    5. // Created by Stefan Wahrendorff on 22.12.12.
    6. // Copyright (c) 2012 Stefan Wahrendorff. All rights reserved.
    7. //
    8. #import "AddReptileViewController.h"
    9. @interface AddReptileViewController ()
    10. @end
    11. @implementation AddReptileViewController
    12. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    13. {
    14. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    15. if (self) {
    16. // Custom initialization
    17. }
    18. return self;
    19. }
    20. - (void)viewDidLoad
    21. {
    22. [super viewDidLoad];
    23. // Do any additional setup after loading the view.
    24. }
    25. - (void)didReceiveMemoryWarning
    26. {
    27. [super didReceiveMemoryWarning];
    28. // Dispose of any resources that can be recreated.
    29. }
    30. - (void)saveNewReptile:(id)sender
    31. {
    32. NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    33. NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    34. NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    35. // If appropriate, configure the new managed object.
    36. // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    37. [newManagedObject setValue:_name forKey:@"name"];
    38. // Save the context.
    39. NSError *error = nil;
    40. if (![context save:&error]) {
    41. // Replace this implementation with code to handle the error appropriately.
    42. // 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.
    43. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    44. abort();
    45. }
    46. }
    47. - (void)cancelAddNewReptile:(id)sender{
    48. [self dismissViewControllerAnimated:YES completion:NO];
    49. }
    50. #pragma mark - Fetched results controller
    51. - (NSFetchedResultsController *)fetchedResultsController
    52. {
    53. if (_fetchedResultsController != nil) {
    54. return _fetchedResultsController;
    55. }
    56. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    57. // Edit the entity name as appropriate.
    58. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Reptiles" inManagedObjectContext:self.managedObjectContext];
    59. [fetchRequest setEntity:entity];
    60. // Set the batch size to a suitable number.
    61. [fetchRequest setFetchBatchSize:20];
    62. // Edit the sort key as appropriate.
    63. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    64. NSArray *sortDescriptors = @[sortDescriptor];
    65. [fetchRequest setSortDescriptors:sortDescriptors];
    66. // Edit the section name key path and cache name if appropriate.
    67. // nil for section name key path means "no sections".
    68. NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    69. aFetchedResultsController.delegate = self;
    70. self.fetchedResultsController = aFetchedResultsController;
    71. NSError *error = nil;
    72. if (![self.fetchedResultsController performFetch:&error]) {
    73. // Replace this implementation with code to handle the error appropriately.
    74. // 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.
    75. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    76. abort();
    77. }
    78. return _fetchedResultsController;
    79. }
    80. @end
    Alles anzeigen


    Quellcode

    1. //
    2. // AppDelegate.m
    3. // Reptiles0-0-3
    4. //
    5. // Created by Stefan Wahrendorff on 22.12.12.
    6. // Copyright (c) 2012 Stefan Wahrendorff. All rights reserved.
    7. //
    8. #import "AppDelegate.h"
    9. #import "AddReptileViewController.h"
    10. @implementation AppDelegate
    11. @synthesize managedObjectContext = _managedObjectContext;
    12. @synthesize managedObjectModel = _managedObjectModel;
    13. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
    14. ...
    15. - (void)saveContext
    16. {
    17. NSError *error = nil;
    18. NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    19. if (managedObjectContext != nil) {
    20. if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
    21. // Replace this implementation with code to handle the error appropriately.
    22. // 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.
    23. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    24. abort();
    25. }
    26. }
    27. }
    28. #pragma mark - Core Data stack
    29. // Returns the managed object context for the application.
    30. // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
    31. - (NSManagedObjectContext *)managedObjectContext
    32. {
    33. if (_managedObjectContext != nil) {
    34. return _managedObjectContext;
    35. }
    36. NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    37. if (coordinator != nil) {
    38. _managedObjectContext = [[NSManagedObjectContext alloc] init];
    39. [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    40. }
    41. return _managedObjectContext;
    42. }
    43. // Returns the managed object model for the application.
    44. // If the model doesn't already exist, it is created from the application's model.
    45. - (NSManagedObjectModel *)managedObjectModel
    46. {
    47. if (_managedObjectModel != nil) {
    48. return _managedObjectModel;
    49. }
    50. NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Reptiles0_0_3" withExtension:@"momd"];
    51. _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    52. return _managedObjectModel;
    53. }
    54. // Returns the persistent store coordinator for the application.
    55. // If the coordinator doesn't already exist, it is created and the application's store added to it.
    56. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    57. {
    58. if (_persistentStoreCoordinator != nil) {
    59. return _persistentStoreCoordinator;
    60. }
    61. NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Reptiles0_0_3.sqlite"];
    62. NSError *error = nil;
    63. _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    64. if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    65. /*
    66. Replace this implementation with code to handle the error appropriately.
    67. 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.
    68. Typical reasons for an error here include:
    69. * The persistent store is not accessible;
    70. * The schema for the persistent store is incompatible with current managed object model.
    71. Check the error message to determine what the actual problem was.
    72. 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.
    73. If you encounter schema incompatibility errors during development, you can reduce their frequency by:
    74. * Simply deleting the existing store:
    75. [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
    76. * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
    77. @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
    78. Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
    79. */
    80. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    81. abort();
    82. }
    83. return _persistentStoreCoordinator;
    84. }
    85. #pragma mark - Application's Documents directory
    86. // Returns the URL to the application's Documents directory.
    87. - (NSURL *)applicationDocumentsDirectory
    88. {
    89. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    90. }
    91. @end
    Alles anzeigen
  • Den Source hab' ich nur überflogen, aber

    StefanMe schrieb:

    '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Reptiles

    ist eigentlich ja ziemlich klar: Guck' doch mal, welchen Wert "context" bei dem Methodenaufruf hat… ich würde vermuten, die Liste ist auch noch leer, so dass man nicht sehen kann, ob der FetchRequest überhaupt funktioniert.