从后台恢复后访问核心数据NSManagedObject会导致应用程序崩溃 [英] Accessing a core data NSManagedObject after resuming from background crashes the app

查看:138
本文介绍了从后台恢复后访问核心数据NSManagedObject会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用核心数据,发现应用程序有时会在从后台恢复后崩溃。我已经确定崩溃发生在块方法体内,当我尝试访问一个属性在 NSManagedObject 子类。

I'm using core data and have found the app sometimes crashing after resuming from background. I've identified the crash occurring inside a block method body when I try to access a property on a NSManagedObject subclass.

我有一个属性,引用 NSManagedObject 子类。

I have a property which holds a reference to a NSManagedObject subclass.


@property(nonatomic,strong)CalItem * calObject;

@property(nonatomic,strong) CalItem *calObject;


b $ b

为了重现崩溃,我首先需要调用子viewController( NoteViewController )传递一个块( NoteTextBlock

To reproduce the crash I first need to call the child viewController(NoteViewController) passing a block (NoteTextBlock).

NoteViewController *noteViewController = [[NoteViewController alloc]initWithNote:self.calObject.note NoteTextBlock:^(NSString *noteText) {
                    self.calObject.note = noteText;  //crashing here
                }];

然后将应用程序发送到后台并恢复。
然后在NoteViewController中,我将返回一个消息到调用viewController。

Then send the app to background and resume it. Afterwards in the NoteViewController I'll return a message to the calling viewController.

if (self.noteTextBlock)
{
 self.noteTextBlock(trimmedString);
}

当块返回并且 self.calObject .note = noteText 得到执行的应用程序崩溃。

When the block returns and the line self.calObject.note = noteText gets executed the app crashes.

所以显然你不能把一个块放在堆栈,然后继续在块内定义什么?或者我只是在这里做错了吗?

So apparently you can't put a block on the stack, quite and resume the app and then continue with what was defined inside the block ? Or am I just doing something wrong here ?

编辑

***应用程序由于未捕获异常NSObjectInaccessibleException,原因:CoreData无法为'0xb253100< x-coredata:// C2304B7C-7D51-4453-9993-D33B9113A7A5 / DTODay / p57>'

这个块在子viewController中是这样定义的:

The block is defined like this inside the child viewController:

@property(nonatomic, copy)NoteTextBlock noteTextBlock;

Edit2

这是我得到的我在其崩溃的行上设置了断点。

(lldb)po self.calObject

$ 2 = 0x0b4464d0< DTODay:0xb4464d0> (entity:DTODay; id:0xb489d00< x-coredata:// C2304B7C-7D51-4453-9993-D33B9113A7A5 / DTODay / p57> ;; data:< fault>)

我使用MagicalRecord库来管理所有的核心数据。

I'm using the MagicalRecord lib to manage all the Core Data stuff.

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if ([NSManagedObjectContext MR_defaultContext] == nil
        || [NSManagedObjectModel MR_defaultManagedObjectModel] == nil
        || [NSPersistentStoreCoordinator MR_defaultStoreCoordinator] == nil
        || [NSPersistentStore MR_defaultPersistentStore] == nil
        )
    {
        //coming back from background, re-init coredata stack
        [MagicalRecordHelpers setupCoreDataStackWithAutoMigratingSqliteStoreNamed:DBNAME];
    }


推荐答案

我不熟悉MagicalRecords ,但是...

I am not familiar with MagicalRecords, but ...

当您有无错的时会引发此异常(如在Edit2中可以看到的)在商店中不再存在(或从不存在)的对象。

在以下情况下可能会发生这种情况:

This exception is raised when you have an un-faulted (as can be seen in Edit2) object that no longer (or never had) exists in the store.
This might happen in a few cases:


  1. 另一个上下文已将其从商店中删除

  2. 您已插入它,获得了永久ID,并且:
    objectWithID:



$ b b

如果您可以描述您的堆栈结构,以及您的物件状态/状态,可能还有其他情况,我忘记了或不知道。

There might be other cases i'm forgetting, or unaware of.

起源我们可能能够更好地理解问题

if you could describe your stack structure, and your object state/origin we might be able to understand the problem better

这篇关于从后台恢复后访问核心数据NSManagedObject会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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