How to handle a CoreData crash in iOS Swift? - ios

I need to handle a core data crash. My code got crashed on the managedObjectContext.save().
But catch block did not catch any exception. To avoid the crash how can I write my Catch block better Here is my code.
do {
try managedObjectContext.save()
}
catch let error as NSError {
Print.print("Error saving data store: \(error)")
}

This is the sample for saving data using CoreData .This may helps you .
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if let entity = NSEntityDescription.entity(forEntityName: "Employees", in: context){
let myItem = NSManagedObject(entity: entity, insertInto: context)
myItem.setValue(nameTF.text, forKey: "names")
myItem.setValue(15655, forKey: "mobileNo")
do {
try context.save()
}catch let nserror as NSError{
print("ERROR: Coredata error \(nserror)")
}
}

This really looks like you messed up with your initialization of context, persistent store and its coordinator. You would do best looking into that. There are similar posts already on SO like this one.
More importantly you will not intercept such exception with try-catch in Swift. Realistically the Swift try-catch does not have anything to do with exceptions but is a high level API for you to intercept reported errors. In your case you just intercept the error that may be reported when saving your data into database. But the error came from a bit deeper as it seems.
To go a step further the whole core data is still completely in objectiveC which has completely different system for throwing exceptions and although those exceptions MAY be intercepted with objectiveC try-catch the same exception will not be intercepted by the one from Swift. What this system did is only replaced entering of pointer to error into method: .save(&error) which was used in objectiveC. And your catch block will trigger only when this error is non-null.

Related

NSMergeConflict with oldVersion and newVersion reported as being equal

I recently managed to catch an extremely rare exception in my code and was hoping folks here could help me understand it. I have a game which is tracking elapsed time and persisting the value to Core Data every second. At the same time a user could be playing the game and causing the score to update, which is also saved in Core Data. I suspect that I should be serializing saves, but at the moment I'm not. Everything works great 99.99% of the time, but once in a while the call to context.save() throws an exception.
I have a merge policy set as so:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
And here's how I save elapsed time. The same pattern is used for updating a player's score.
func saveElapsedTime() {
let container = (UIApplication.shared.delegate as! AppDelegate).persistentContainer
container.performBackgroundTask() { (context) in
let storedGame = context.object(with: self.storedGameID) as! StoredGame
storedGame.elapsedTime = Int32(self.elapsedTime)
do {
try context.save()
} catch let nserror as NSError {
print("saveElapsedTime: failed to save: \(nserror). userInfo: \(nserror.userInfo).")
}
}
}
The rare error I was able to catch looks like this:
Error Domain=NSCocoaErrorDomain Code=133020 "Could not resolve merge changes."
<snip>...with oldVersion = 463 and newVersion = 463...<snip>
I've seen plenty of posts where the old and new versions are off by one or a small number, and people recommend setting a merge policy which is different than the default. I'm guessing that in my case where oldVersion == newVersion it's because multiple threads are trying to save at the exact same time. Can someone confirm this just by looking at the error message? Should I be serializing all saves using an OperationQueue like in this example: NSPersistentContainer concurrency for saving to core data?
Thanks in advance.

CoreData error: API Misuse: Attempt to serialize store access on non-owning coordinator

Note:
This post does not apply since I actually use CoreData.
In this post the last answer suggests to fetch all items in the new background thread before adding new objects, but this is done in my code.
This post suggests to unfault an item before its context is saved, but this is also done in my code.
My app uses CoreData to store objects called shoppingItems. I wrote a class CoreDataManager that initialises CoreData and has essentially one function to overwrite the currently stored items, and one function to fetch all items. Both functions operate in the background, i.e. on a separate thread.
Here is my code (irrelevant parts are left out).
I setup core data on the main thread:
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: modelName)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
})
return container
}()
This is the write function:
func overwriteShoppingItems(_ shoppingItems: Set<ShoppingItem>, completion: #escaping (Error?) -> Void) {
let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
let viewContext = self.persistentContainer.viewContext
backgroundContext.parent = viewContext
backgroundContext.performAndWait {
// Delete currently stored shopping items
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: CDEntityShoppingItem)
do {
let result = try backgroundContext.fetch(fetchRequest)
let resultData = result as! [NSManagedObject]
for object in resultData {
backgroundContext.delete(object)
}
if !shoppingItems.isEmpty {
// Save shopping items in managed context
let cdShoppingItemEntity = NSEntityDescription.entity(forEntityName: CDEntityShoppingItem, in: backgroundContext)!
for nextShoppingItem in shoppingItems {
let nextCdShoppingItem = CDShoppingItem(entity: cdShoppingItemEntity, insertInto: backgroundContext)
nextCdShoppingItem.name = nextShoppingItem.name
}
}
let saveError = self.saveManagedContext(managedContext: backgroundContext)
completion(saveError)
} catch let error as NSError {
completion(error)
}
}
}
func saveManagedContext(managedContext: NSManagedObjectContext) -> Error? {
if !managedContext.hasChanges { return nil }
do {
try managedContext.save()
return nil
} catch let error as NSError {
return error
}
}
And this is the fetch function:
func fetchShoppingItems(completion: #escaping (Set<ShoppingItem>?, Error?) -> Void) {
let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
let viewContext = self.persistentContainer.viewContext
backgroundContext.parent = viewContext
backgroundContext.performAndWait {
let fetchRequest: NSFetchRequest<CDShoppingItem> = CDShoppingItem.fetchRequest()
do {
let cdShoppingItems: [CDShoppingItem] = try backgroundContext.fetch(fetchRequest)
guard !cdShoppingItems.isEmpty else {
completion([], nil)
return
}
for nextCdShoppingItem in cdShoppingItems {
}
completion(shoppingItems, nil)
} catch let error as NSError {
completion(nil, error)
}
}
}
In normal operation, the code seems to work.
The problem:
I also wrote a unit test that tries to provoke multi-threading problems. This test uses a concurrent dispatch queue:
let concurrentReadWriteQueue = DispatchQueue(label: „xxx.test_coreDataMultithreading", attributes: .concurrent)
A timer defines the test time.
In the scheme for tests, I have set the arguments -com.apple.CoreData.Logging.stderr 1 and -com.apple.CoreData.ConcurrencyDebug 1.
During the test time, overwriteShoppingItems and fetchShoppingItems are inserted repeatedly in the queue, and executed concurrently.
This unit test executes a few reads and writes, before it stops at the line
let itemName = nextCdShoppingItem.name!
because nextCdShoppingItem.name is nil, which should never happen, because I never store nil.
Immediately before the crash, the following is logged:
CoreData: error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600000e6c980, store PSC = 0x0)
If I do only fetches, or only writes, the CoreData warning is not logged. Thus it seems definitely to be a multi-threading issue.
However, CoreData.ConcurrencyDebug does not detect it.
It looks as if during a fetch operation on one thread, the other thread deletes the currently fetched item, so that its properties are read back as nil.
But this should not happen because fetches and saves are done with backgroundContext.performAndWait, i.e. serially.
And the stack trace shows that only a single thread accesses CoreData: Thread 3 Queue : NSManagedObjectContext 0x600003c8c000 (serial)
My questions:
What is my misuse of the CoreData API, and how to do it right?
Why is an item sometimes read back as nil?
EDIT:
Maybe this helps to identify the problem: When I comment out backgroundContext.delete(object) in overwriteShoppingItems, the error is no longer logged, and no item is fetched as nil.
Okay so i got same error but only while "archiving" the project. Took me several hours to find the issue, since I was getting the error only on uploading, in the assets catalog, I had some duplicate images, by removing them the error is gone.
Anyone else getting this error AND your Coredata setup is fine, check your assets folder.
If you are not using core data in your project then check your assets files are proper or not.
I am also getting error like this then i have findout this error from my project.
Problem seems to be solved.
It happened apparently, because the functions overwriteShoppingItems and fetchShoppingItems both setup a separate background context with let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) with their own queue so that fetches and saves were not serialized by a single queue.
I modified my code now in the following way:
I have now a property
let backgroundContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
that is initialized as
self.backgroundContext.persistentStoreCoordinator = self.persistentContainer.persistentStoreCoordinator
self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
and fetches and saves are done using
backgroundContext.perform {…}
Now the CoreData error is no longer logged, and no item is fetched as nil.
In my case I created an new managedObjectContext without (yet) assigning a persistent store. Eventually I called -[psc metadataForPersistentStore:store] with the store being nil. This caused the log message to be written.
(side-note: you can always catch these outputs by setting a breakpoint on fprintf)
If you are seeing this during tear-down of a Core Data stack, check for un-removed observers or memory leaks.
In my case, I had managed objects which were each observing themselves – one of their relationships, using Swift's Key-Value Observing (NSKeyValueObservation objects). (Example use case: When the last a department's employees are deleted, delete the department.) The console messages appeared while the containing Core Data document was being closed. By breaking on fprintf I could see that the message was logged when a notification was being sent to an observer, and the number of messages was always equal to the number of non-faulted such objects in the managed object context (5 departments --> 5 console messages, etc., etc.). The problem was that I was not removing these observations, and the solution was of course to implement func willTurnIntoFault() in the observing objects, and in this func remove the observation.
In another situation, these messages stopped after I fixed a memory leak, but I did not study that in detail.
The full error messages were: error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600002c5cc00, store PSC = 0x0)
CoreData: error: API Misuse: Attempt to serialize store access on non-owning coordinator (PSC = 0x600002c5cc00, store PSC = 0x0) and they occurred during the call to NSPersistentStoreCoordinator.remove(_:). So, this error is telling me that the store's PSC was nil, but I checked this with the debugger and found that the store's PSC was not nil before the call. I filed a bug to Apple on this (FB11669843) asking if they could improve the error message.

How to add NSManagedObjectContext with ConcerrencyType in swift 3?

In my app i have tasks that download data and save them to CoreData. After i migrated to swift 3 it started throwing exceptions while saving data, at a random time. As i understood, it can happen if i use one context for all operations. Now i created another context with concurrency type, everything works without errors, but nothing is saved to .sqlite file :)
Here's how i create context:
static let context : NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
static let privateContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
static func declare()
{
AixmParser.privateContext.parent = AixmParser.context
}
I save data like this:
do{
try privateContext.save()
}
catch{
}
Do i have to add something to declaration or to the core data stack?
Update: Core Data stack below.
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "AppName")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
container.viewContext.perform({
// actions upon the NSMainQueueConcurrencyType NSManagedObjectContext for this container
})
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
So first is that you have to create a link from the privateContext to the NSPersistentContainer's viewContext. So, while creating the private,
privateContext, privateContext.parentContext = viewContext. While saving
let context = privateContext
while(context != nil) {
context.save()
context = context.privateContext
}
This will work.
I am not sure why did you get the errors in the first place.

Core Data error code=134030 "An error occurred while saving" with no userInfo

I have an app built in Swift 3 using Xcode 8 GM seed. The app has a keyboard extension. I'm using the shared group directory for the sqlite db location, and database access is done using a framework. Things are working until I try to call SaveContext() from the keyboard. I get the error mentioned in the title error code=134030. The message says "An error occurred while saving" and there is no additional information. There's no userInfo for me to get at the underlying error.
I've dumped out some debug information to make sure I'm looking at the same instance of the same item that I'm trying to update. It's a very simple update, just incrementing a use counter when something is used. Is there some problem with saving from a keyboard extension? Do they open in read-only mode?
Here's the bulk of the CoreDataStack:
let coreDataStack = CoreDataStack()
public class CoreDataStack {
public func saveContext()
{
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch let error {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
// MARK: - Private
lazy var managedObjectContext: NSManagedObjectContext = {
return self.persistentContainer.viewContext
}()
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let modelUrl = Bundle(identifier: "com.myapp.AppDataKit")?.url(forResource: "MyApp-SE", withExtension: "momd")
let managedObjectModel = NSManagedObjectModel(contentsOf: modelUrl!)!
let container = NSPersistentContainer(name: "MyApp-SE", managedObjectModel: managedObjectModel)
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: try! FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myapp")!.appendingPathComponent("MyAppSEData.sqlite"))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
} // class CoreDataStack
In the catch I'm examining error and I can't get any more info from it. After casting it to NSError it still doesn't help since userInfo is an empty dict.
I haven't implemented the error handling as yet, but I can see the items in the keyboard so there's no issues with the retrieval. Only when saving does it crash.
The place where I'm attempting the update is ridiculously simple:
item.useCount = NSNumber(value: item.useCount.intValue + 1)
Logger.add(item.debugDescription)
AppData.SaveContext()
AppData is a class with static functions to access core data
public class AppData {
public static func SaveContext() {
coreDataStack.saveContext()
}
...
}
I looked at the device log and didn't get any better information there. I can see the two lines in my data kit and then two from libswiftCore.dylib
0 libswiftCore.dylib 0x0000000100ab2848 0x100a80000 + 206920
1 libswiftCore.dylib 0x0000000100ab2848 0x100a80000 + 206920
Anyone know how to symbolicate libswiftcore in Xcode8?
Thanks,
Mike
I figured it out. Turns out that it was because the "Allow Full Access" option for the keyboard was turned off. So if you want to be able to write to the shared group folder, including updating a sqlite db, you have to install the app & keyboard and check the "Allow Full Access" option first. Then you can return to Xcode and run the extension in debug mode. The setting is retained and everything should work.
Mike

EXC_BAD_ACCESS(code=1, address=0x10) on Core Data Fetch

I'm getting an intermittent bug that is proving very hard to debug.
I'm getting the following error from the following method
EXC_BAD_ACCESS(code=1, address=0x10) on Core Data Fetch
class func getAll(context: NSManagedObjectContext) -> [Tag] {
var returnValue: [Tag] = []
do {
let fetchRequest = NSFetchRequest(entityName: Tag.entityName())
returnValue = try context.executeFetchRequest(fetchRequest) as! [Tag]
} catch {
}
return returnValue
}
The bug is extermely intermittent, and is only happening on every few 100 sessions, but is appearing frequent enough that I need to deal with it. The code breaks on the line returnvalue = try context.execute...
From debugging, my fetchRequest is not nil
my context is not nil
returnValue has a default value of an empty array
my backgroundContext is running on a background thread
I've turned on the NSZombieFlag to try to see if some memory is deallocated somewhere and then being accessed, but I'm stumped on what is causing this. Any ideas or insight would be much appreciated.
Almost all EXC_BAD_ACCESS issues I've seen with Core Data are caused by trying to use thread concurrency instead of the newer queue concurrency model.
Since iOS 5 you are required to use performBlock or performBlockAndWait when accessing a managed object context.
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Concurrency.html

Resources