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

Posted

技术标签:

【中文标题】扩展教程材料时 entityForName 和 ManagedObjectContext 的问题【英文标题】:Problem with entityForName & ManagedObjectContext when extending tutorial material 【发布时间】:2010-04-18 19:58:28 【问题描述】:

下午好,

我尝试将第二个数据实体添加到 (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】:

它有助于发布您遇到的错误。

您是否尝试过以下方法:

干净构建您的项目 重置模拟器以删除旧的 sqlite 数据文件

如果这些都不能解决问题,那么将您收到的错误发布为您的问题的更新。

更新

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

【讨论】:

感谢您的建议,我没有找到重置模拟器选项,并认为删除应用程序会删除所有相关数据。重置似乎做得更彻底,代码现在运行没有错误。问候,马丁。 我还不完全熟悉调试器,我发布的最有用的信息是什么,我在哪里可以找到这些信息?马丁

以上是关于扩展教程材料时 entityForName 和 ManagedObjectContext 的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 NSInternalInconsistencyException',原因:'+entityForName:失败报告

abaqus中后处理中U的单位是啥?

powerquery扩展到新行与提取值区别

NSInternalConsistencyException 原因 +entityForName:找不到实体名称的 NSManagedObjectModel

+entityForName: 找不到实体名称的 NSManagedObjectModel

Swift 3 核心数据 NSInternalInconsistencyException entityForName 无法在此模型中找到名为 Foo 的实体