I have main context in controller:
let mainContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContextThen I create private context in my operation with passing reference to mainContext:
privateContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateContext.parentContext = self.mainContext
privateContext.performBlock {
let someObj = NSEntityDescription.insertNewObjectForEntityForName(SomeObj.entityName, inManagedObjectContext: privateContext) as! SomeObj
someObj.prop1 = "some1"
someObj.prop2 = "some2"
...
if privateContext.hasChanges {
do {
try privateContext.save()
}
catch let saveError as NSError {
finishWithError(saveError)
}
}
}And then I get saved items from my controller:
var someObjArr = [SomeObj]()
let mainContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let someObjRequest = NSFetchRequest(entityName: SomeObj.entityName)
do {
someObjArr = try mainContext.executeFetchRequest(someObjRequest) as! [SomeObj]
print(someObjArr)
} catch {
print(error)
}And this works - I have array of SomeObj from data store.
But if I restart application and try to get this array without presaving data I have empty array.
Why so? What's wrong in my steps? Why does data disappear after application restart being saved before.