iphone - 扩展教程 Material 时 entityForName 和 ManagedObjectContext 的问题

标签 iphone ios core-data

大家下午好

我尝试在 (locations) 核心数据教程代码中向持久存储添加第二个数据实体,然后在新 View 中访问它。我认为我已经按照教程进行操作,并检查了我是否在进行干净的构建等,但看不到要更改什么以防止它崩溃。

恐怕我对此无能为力,似乎无法找到我错过的步骤。我已经在下面粘贴了头文件和代码文件,如果我需要共享更多代码,请告诉我。崩溃似乎在线上发生:

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

目前代码中还有另外一行引用 galleryviewcontroller,它在主应用程序委托(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

最佳答案

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

您是否尝试过以下方法:

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

如果这些都不能解决问题,请发布您遇到的错误作为问题的更新。

更新

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

关于iphone - 扩展教程 Material 时 entityForName 和 ManagedObjectContext 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2663727/

相关文章:

iphone - UIWindow makeKeyAndVisible 与 ARC 配合不佳

iphone - sectionNameKeyPath 通过多重关系

iphone - 我可以将 2 个 nib 文件连接到同一个 .h 和 .m 吗?

ios - 如何将嵌套的 json 字符串正确解析为正确的 json?

ios - NSMutableArray 保存所有 View 的所有值

ios - WatchKit 和应用程序之间核心数据实时同步

iphone - 核心数据 : Not able to add persistent store to coordinator

iphone - 绘制自定义 UINavigationBar。为什么此代码在视网膜显示中不起作用?

ios - SKProductsRequest 返回 0 个产品的原因?

ios - NSPredicate 和 BeginsWith