0

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

这是控制台消息:

CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获到异常。这通常是 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

感谢您的兴趣:-)

4

0 回答 0