I have an InMemory Store Coordinator declared like so:
lazy var ramStoreCoordinator: NSPersistentStoreCoordinator = { // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added 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. // Create the coordinator and store let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) var failureReason = "There was an error creating or loading the application's saved data." do { try coordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil) } catch { // Report any error we got. var dict = [String: AnyObject]() dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" dict[NSLocalizedFailureReasonErrorKey] = failureReason dict[NSUnderlyingErrorKey] = error as NSError let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) // Replace this with code to handle the error appropriately. // abort() 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. NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") abort() } return coordinator }()
as well as an associated ManagedObjectContext:
lazy var ramManagedObjectContext: NSManagedObjectContext = { // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. let coordinator = self.ramStoreCoordinator var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) managedObjectContext.persistentStoreCoordinator = coordinator managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy return managedObjectContext }()
I'm trying to execute a fetch request like so:
let fetchRequest = NSFetchRequest(entityName: "Post") let batchDelete = NSBatchDeleteRequest(fetchRequest: fetchRequest) do { // Execute Batch Request try ramManagedObjectContext.executeRequest(batchDelete) } catch { let updateError = error as NSError print("\(updateError), \(updateError.userInfo)") }
the line:
try ramManagedObjectContext.executeRequest(batchDelete)
crashes the app with the following output:
2016-04-30 23:47:40.271 Secret[2368:1047869] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unknown command type (entity: EntityName; predicate: ((null)); sortDescriptors: ((null)); type: NSManagedObjectIDResultType; ) >' * First throw call stack: (0x18145ae38 0x180abff80 0x1833710b0 0x18338991c 0x183391d64 0x101121a3c 0x10112d5f0 0x1833845bc 0x1832c1d5c 0x183354e04 0x10011abc4 0x10011947c 0x100092bf0 0x10009269c 0x1000926ec 0x186a1aac0 0x186a1b258 0x186901854 0x186904a4c 0x1866d4fd8 0x1865e0014 0x1865dfb10 0x1865df998 0x183f4da20 0x101121a3c 0x1011274e4 0x181410dd8 0x18140ec40 0x181338d10 0x182c20088 0x18660df70 0x1000fcba8 0x180ed68b8) libc++abi.dylib: terminating with uncaught exception of type NSException
3 Answers
Answers 1
NSBatchDeleteRequest should be executed on your ramStoreCoordinator, not ramManagedObjectContext, since it works directly with NSPersistenceStore class instance:
try persistentStoreCoordinator.executeFetchRequest(batchDelete, withContext: ramManagedObjectContext)
Check this link for more details: https://developer.apple.com/videos/play/wwdc2015/220/
Hope it helped)
Answers 2
I had the exact same problem. Solved it by changing the in memory store to NSSQLiteStoreType . Have not done any research why this happens to in memory store, but I hope that solves your problem.
Answers 3
This error can occur when you rename some files outside XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference") You re-import the files in your project and everything will be ok !
If it didn't fix, try this
try persistentStoreCoordinator.executeFetchRequest( batchDelete, withContext:context )
as NSBatchDeleteRequest
is executed on the persistent store coordinator, not the managed object context.
0 comments:
Post a Comment