我正在使用核心數據觀察iOS中的一個奇怪的錯誤。 基本上,我的設置如下 - 有2個NSManagedObjectContexts,tempContext
和mainContext
。 我使用tempContext
作爲創建和編輯實體的臨時「暫存器」,當我想要保存它們時,我將它移動到mainContext
以保存。 請注意,mainContext
不是tempContext
的母公司。如下 -在上下文之間移動對象時,iOS coredata objectWithID中的錯誤
[1] newEntity.property1 = @"SomeProperty";
newEntity.managedObjectContext // this is tempContext currently
[2] saveEntityInTempContext
[3] newEntity = (Entity *)[mainContext objectWithID:newEntity.objectID];
[4] [mainContext insertObject:newEntity];
現在,後插入[4],如果我檢查newEntity.property1
,它被設置爲nil
此舉發生。 但是,如果我在[3]之後,然後在[4]之後首先檢查屬性,它將正確顯示爲「SomeProperty」。我不明白如何強迫失敗應該在這裏有所作爲。
我的目標是將實體從一個上下文移動到另一個上下文。
'[mainContext objectWithID:newEntity.objectID]'從主要上下文中返回一個對象。我認爲不需要(也可能是錯誤的)在該對象上調用insertObject,它已經被插入。 –
您是如何在首位創建'newEntity'的?它是否已經插入到'tempContext'中?此外,當此代碼執行時,更改已保存在'tempContext'上? –
@TomHarrington是的,'''''newEntity'''已經被插入到''tempContext'''中,步驟[2]表明這個實體被保存在''tempContext'''中。 – Devang