entityForName& ManagedObjectContext时扩展教程材料 [英] Problem with entityForName & ManagedObjectContext when extending tutorial material

查看:82
本文介绍了entityForName& ManagedObjectContext时扩展教程材料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午所有



我试图在(位置)coredata教程代码中向持久存储添加第二个数据实体,然后在新视图中访问。我认为我遵循了教程,并检查我正在做一个干净的构建等,但不能看到要更改,以防止崩溃。



我恐怕我的智慧结束了这一个,似乎找不到我错过的步骤。我粘贴了下面的标题和代码文件,请让我知道,如果我需要共享任何更多的代码。崩溃似乎发生在行上:

  NSEntityDescription * entity = [NSEntityDescription entityForName:@AlbuminManagedObjectContext:[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

  #importRootViewController.h
#importLocationsAppDelegate.h
#importAlbum.h
# importGalleryViewController.h
#importImage.h


@Implementation GalleryViewController

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

/ *
//指定的初始化程序。如果以编程方式创建控制器并想要执行不适合viewDidLoad的自定义,则覆盖。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])){
//自定义初始化
}
return self;
}
* /
- (void)updateRowNumber:(int)theIndex {
rowNumber = theIndex;
LocationsAppDelegate * mainDelegate =(LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
Album * anAlbum = [mainDelegate.albumsArray objectAtIndex:rowNumber];
lblMessage.text = anAlbum.uniqueAlbumIdentifier;
}



//加载视图之后,通常使用nib来实现viewDidLoad进行其他设置。
- (void)viewDidLoad {
[super viewDidLoad];
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addImage)];
addButton.enabled = YES;
self.navigationItem.rightBarButtonItem = addButton;

/ *在另一个答案中找到这个,将它添加到代码没有帮助。
if(managedObjectContext == nil){
managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];
}
* /

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

//按创建日期,最近的顺序排序相册。
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@imagePathascending:NO];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

//执行fetch - 创建一个可变的结果副本。
NSError * error = nil;
NSMutableArray * mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:& error] mutableCopy];
if(mutableFetchResults == nil){
//处理错误。
}

[self setImagesArray:mutableFetchResults];

int a = 5;
int b = 10;

for(int i = 0; i <[ImageArray 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];
[任何版本];
a + = 80;
}

}
- (void)addImage {
NSString * msg = [NSString stringWithFormat:@%i,rowNumber];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@添加图片到消息:msg
委托:self cancelButtonTitle:@NootherButtonTitles:@Yes,nil]
[alert show];
[alert release];
}

- (void)didReceiveMemoryWarning {
//如果没有超级视图,则释放视图。
[super didReceiveMemoryWarning];

//释放所有未使用的缓存数据,图像等。
}

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


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


@end




您尝试过以下操作:


$ b $

b

  • 清理您的项目

  • 重置模拟器以删除旧的sqlite数据文件



如果这两个问题都不能解决问题,请将您收到的错误信息作为您的问题的更新。



更新



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


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

解决方案

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.

这篇关于entityForName&amp; ManagedObjectContext时扩展教程材料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆