entityForName和amp;的问题扩展教程材料时的ManagedObjectContext

时间:2010-04-18 19:58:28

标签: iphone ios core-data

下午所有,

我尝试将第二个数据实体添加到(locations)coredata教程代码中的持久性存储中,然后在新视图中访问它。我认为我已经按照教程进行了检查,并检查了我是否正在进行干净的构建等,但是无法看到要更改的内容以防止崩溃。

我担心我会因为这个而斗智斗勇,似乎无法找到我错过的那一步。我已粘贴下面的标题和代码文件,如果我需要分享更多代码,请告诉我。崩溃似乎发生在线上:

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

目前代码中还有另一行引用了galleryviewcontroller,这是在主应用程序委托中:

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 个答案:

答案 0 :(得分:2)

发布您遇到的错误会有所帮助。

您是否尝试过以下操作:

  • 清理项目构建
  • 重置模拟器以删除旧的sqlite数据文件

如果这些都没有解决问题,请将您收到的错误作为问题的更新发布。

更新

我正在寻找的信息将在控制台中打印出来,而调试器是一个无限有用的工具,我建议学习它,在这种情况下,您的问题可以通过查看应用程序的控制台输出来解决。 / p>