Note that there are some explanatory texts on larger screens.

plurals
  1. POCrash Occurring on First Launch When populating core data database
    text
    copied!<p>I keep getting an error in the debugger for my application saying,</p> <blockquote> <p>2013-06-23 16:07:15.826 collection view recipies[5681:c07] -[NSManagedObject length]: unrecognized selector sent to instance 0x9495280 2013-06-23 16:07:15.827 collection view recipies[5681:c07] <strong>* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject length]: unrecognized selector sent to instance 0x9495280' *</strong> First throw call stack: (0x26ac012 0x1517e7e 0x27374bd 0x269bbbc 0x269b94e 0x2b11c4 0x16d80a 0x4464 0x64f2da 0x6508f4 0x652b91 0x19c2dd 0x152b6b0 0x18eefc0 0x18e333c 0x18eeeaf 0x23b2bd 0x183b56 0x18266f 0x182589 0x1817e4 0x18161e 0x1823d9 0x1852d2 0x22f99c 0x17c574 0x17c76f 0x17c905 0x185917 0x14996c 0x14a94b 0x15bcb5 0x15cbeb 0x14e698 0x2c06df9 0x2c06ad0 0x2621bf5 0x2621962 0x2652bb6 0x2651f44 0x2651e1b 0x14a17a 0x14bffc 0x1e9d 0x1dc5) libc++abi.dylib: terminate called throwing an exception</p> </blockquote> <p>In My application delegate, if check to see if the application is being launched for the first time, and if it is, I then add several image paths to the core data structure. In AppDelegate.m under ApplicationDidFinishLaunchingWithOptions,</p> <pre><code> if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) { // app already launched } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; // This is the first launch ever NSArray *mainDishImages = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", nil]; NSArray *drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg", @"white_chocolate_donut.jpg", nil]; for (NSString *imagePath in mainDishImages) { NSManagedObjectContext *context = [self managedObjectContext]; NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:context]; [newRecipe setValue:imagePath forKey:@"imageFilePath"]; } for (NSString *imagePath in drinkDessertImages) { NSManagedObjectContext *context = [self managedObjectContext]; NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Deserts" inManagedObjectContext:context]; [newRecipe setValue:imagePath forKey:@"imageFilePath"]; } } </code></pre> <p>And I access that data in my collectionViewController, I access that data.</p> <pre><code>- (NSManagedObjectContext *)managedObjectContext{ NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Deserts"]; deserts = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; NSFetchRequest *fetchRequestTwo = [[NSFetchRequest alloc] initWithEntityName:@"Recipe"]; meals = [[managedObjectContext executeFetchRequest:fetchRequestTwo error:nil] mutableCopy]; recipeImages = [NSArray arrayWithObjects:meals, deserts, nil]; [self.collectionView reloadData]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Deserts"]; deserts = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; NSFetchRequest *fetchRequestTwo = [[NSFetchRequest alloc] initWithEntityName:@"Recipe"]; meals = [[managedObjectContext executeFetchRequest:fetchRequestTwo error:nil] mutableCopy]; recipeImages = [NSArray arrayWithObjects:meals, deserts, nil]; UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout; collectionViewLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0); self.navigationController.navigationBar.translucent = YES; self.collectionView.contentInset = (UIEdgeInsetsMake(44, 0, 0, 0)); selectedRecipes = [NSMutableArray array]; } </code></pre> <p>According to crashalytics, the error is in the line where it says</p> <pre><code>recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]]; </code></pre> <p>In the method</p> <pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"Cell"; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame"]]; cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame-selected.png"]]; return cell; } </code></pre> <p>I hope you can help. Thanks In Advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload