核心数据随机崩溃

时间:2013-07-28 15:45:18

标签: iphone ios core-data nsmanagedobjectcontext

我完成了应用程序的开发,但是当我尝试在核心数据中保存图像时,我遇到了随机崩溃。

以下是控制台消息:

  

CoreData:错误:严重的应用程序错误。抓住了例外   在核心数据更改处理期间。这通常是一个bug   NSManagedObjectContextObjectsDidChangeNotification的观察者。    - [_ PFExternalReferenceData compare:]:使用userInfo(null)发送到实例0x94b69b0的无法识别的选择器

问题在于它是不确定的。它在5次中或多或少发生一次。

有人会知道我应该找哪个地方吗?

编辑:

以下是我班级的代码似乎造成了问题。当我点击保存按钮,调用 - (IBAction)saveResultingImage:

时,有时会发生这种情况
#import "ResultViewController.h"


@interface ResultViewController ()
@property (nonatomic)BOOL resultSaved;
@end

@implementation ResultViewController


#pragma mark - IBActions

- (IBAction)saveResultingImage
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    _resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    _resultSaved = [self saveOnCoreData];

    [self performSegueWithIdentifier:@"BackToStartSegue" sender:self];
}

- (IBAction)discardWithoutSaving
{
    [self performSegueWithIdentifier:@"BackToStartSegue" sender:self];
}

- (IBAction)restartDrawing:(id)sender
{
    UIImage *backgroundImage = _subImage;
    UIImageView *imageView =
    [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    imageView.image = backgroundImage;
    [self.view addSubview:imageView];

    UIImage *maskImage = _maskImage;
    CGRect maskViewRect = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
    ImageMaskView *subView = [[ImageMaskView alloc] initWithFrame:maskViewRect image:maskImage];

    [self.view addSubview:subView];
}

- (IBAction)showExplainVC
{
    [self performSegueWithIdentifier:@"ShowInfoSegue" sender:self];
}


#pragma mark - Methods

- (BOOL)saveOnCoreData
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newMeme =
            [NSEntityDescription insertNewObjectForEntityForName:@"Meme" inManagedObjectContext:context];
    NSData *toSaveMeme = UIImagePNGRepresentation(_resultImage);
    [newMeme setValue:toSaveMeme forKey:@"image"];

    NSError *error = nil;
    if (![context save:&error]) {
        DebugLog(@"Can't save: %@ %@", error, [error localizedDescription]);
        return NO;
    }
    return YES;
}


#pragma mark - View Controller LifeCycle

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"BackToStartSegue"]) {
        return _resultSaved;
    }
    return _resultSaved;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    DebugLog(@"Called viewWillAppear");
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *discardBarButtonItem =
            [[UIBarButtonItem alloc] initWithTitle:@"Discard" style:UIBarButtonItemStylePlain target:self action:@selector(discardWithoutSaving)];
    UIBarButtonItem *saveBarButtonItem =
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveResultingImage)];
    UIBarButtonItem *infoBarButton =
            [[UIBarButtonItem alloc] initWithTitle:@"How to" style:UIBarButtonItemStylePlain target:self action:@selector(showExplainVC)];
    discardBarButtonItem.style = UIBarButtonItemStyleBordered;
    self.navigationItem.rightBarButtonItems = @[saveBarButtonItem, discardBarButtonItem, infoBarButton];

    self.view.backgroundColor = [UIColor blackColor];

    [self restartDrawing:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Core Data Stack

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

@end

感谢您的关注: - )

0 个答案:

没有答案