Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 745159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:03:45+00:00 2026-05-14T09:03:45+00:00

Afternoon all, I tried to add a second data entity to the persistent store

  • 0

Afternoon all,

I tried to add a second data entity to the persistent store in the (locations) coredata tutorial code, and then access this in a new view. I think that I’ve followed the tutorial, and checked that I’m doing a clean build etc, but can’t see what to change to prevent it crashing.

I’m afraid I’m at my wits end with this one, and can’t seem to find the step that I’ve missed. I’ve pasted the header and code files below, please let me know if I need to share any more of the code. The crash seems to happen on the line:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];

There is one other line in the code that refers to galleryviewcontroller at the moment, and that’s in the main application delegate:

galleryViewController.managedObjectContext = [self managedObjectContext];

GalleryViewController.h

#import <UIKit/UIKit.h>


@interface GalleryViewController : UIViewController {
   NSManagedObjectContext *managedObjectContext;        
   int rowNumber;
   IBOutlet UILabel *lblMessage;
   UIBarButtonItem *addButton;
   NSMutableArray *imagesArray;
}

@property (readwrite) int rowNumber;
@property (nonatomic,retain) UILabel *lblMessage;
@property (nonatomic,retain) NSMutableArray *imagesArray;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 

@property (nonatomic, retain) UIBarButtonItem *addButton;

-(void)updateRowNumber:(int)theIndex;
-(void)addImage;

@end

GalleryViewController.m

#import "RootViewController.h"
#import "LocationsAppDelegate.h"
#import "Album.h"
#import "GalleryViewController.h"
#import "Image.h"


@implementation GalleryViewController

@synthesize lblMessage,rowNumber,addButton,managedObjectContext;
@synthesize imagesArray;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/
-(void)updateRowNumber:(int)theIndex{
    rowNumber=theIndex;
    LocationsAppDelegate *mainDelegate =(LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];   
    Album *anAlbum = [mainDelegate.albumsArray objectAtIndex:rowNumber];      
    lblMessage.text = anAlbum.uniqueAlbumIdentifier;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];    
    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addImage)];
    addButton.enabled = YES;
    self.navigationItem.rightBarButtonItem = addButton;

    /* Found this in another answer, adding it to the code didn't help.
    if (managedObjectContext == nil) {
        managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
    }
   */ 

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:[self managedObjectContext]];
    [request setEntity:entity];

    // Order the albums by creation date, most recent first.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"imagePath" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];
    [sortDescriptors release];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
    }

    [self setImagesArray:mutableFetchResults];

    int a = 5;
    int b = 10;

    for( int i=0; i<[imagesArray count]; i++ )
    {
        if( a == 325 )
        {
            a = 5;
            b += 70;
        }
        UIImageView *any = [[UIImageView alloc] initWithFrame:CGRectMake(a,b,70,60)];
        any.image = [imagesArray objectAtIndex:i];
        any.tag = i;
        [self.view addSubview:any];
        [any release];
        a += 80;
    }

}       
-(void)addImage{
    NSString *msg = [NSString stringWithFormat:@"%i",rowNumber];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add image to" message:msg  
                                                       delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
    [lblMessage release];
    [managedObjectContext release];
    [super dealloc];
}


@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T09:03:45+00:00Added an answer on May 14, 2026 at 9:03 am

    It helps to post the error you are getting.

    Have you tried the following:

    • Clean build of your project
    • Resetting the simulator to remove the older sqlite data file

    If neither of those solve the issue then post the error you are getting as an update to your question.

    update

    The information I was looking for would be printing out in the console, while the debugger is an infinitely useful tool and I recommend learning it, in this case your issue would have been resolved by reviewing the console output of your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.