From 96b151f20047653b7badac700b9071ec2a8c2d8d Mon Sep 17 00:00:00 2001 From: Danish Jafri Date: Tue, 30 Jul 2019 18:17:46 +0530 Subject: [PATCH] Fix #1309: Don't use NSBatchDeleteRequest on In-Memory store(#1310) --- Data/models/CRUDProtocols.swift | 9 +++++++-- Data/models/DataController.swift | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Data/models/CRUDProtocols.swift b/Data/models/CRUDProtocols.swift index 5ea013e3393..508e2c9b023 100644 --- a/Data/models/CRUDProtocols.swift +++ b/Data/models/CRUDProtocols.swift @@ -44,9 +44,14 @@ extension Deletable where Self: NSManagedObject { request.includesPropertyValues = includesPropertyValues do { - // NSBatchDeleteRequest can't be used for in-memory store we use in tests. + // NSBatchDeleteRequest can't be used for in-memory store we use in tests and PBM. // Have to delete objects one by one. - if AppConstants.IsRunningTest { + var isInMemoryContext: Bool = false + if let currentCoordinator = context.persistentStoreCoordinator, + let inMemoryCoordinator = DataController.viewContextInMemory.persistentStoreCoordinator { + isInMemoryContext = currentCoordinator == inMemoryCoordinator + } + if AppConstants.IsRunningTest || isInMemoryContext { let results = try context.fetch(request) as? [NSManagedObject] results?.forEach { context.delete($0) diff --git a/Data/models/DataController.swift b/Data/models/DataController.swift index 1e48cdbe113..f6a377b95e2 100644 --- a/Data/models/DataController.swift +++ b/Data/models/DataController.swift @@ -42,7 +42,8 @@ public class DataController: NSObject { // called at higher level when a `.new` WriteContext is passed. task(existingContext) case .new(let inMemory): - let queue = DataController.shared.operationQueue + // Though keeping same queue does not make a difference but kept them diff for independent processing. + let queue = inMemory ? DataController.sharedInMemory.operationQueue : DataController.shared.operationQueue queue.addOperation({ let backgroundContext = inMemory ? DataController.newBackgroundContextInMemory() : DataController.newBackgroundContext() @@ -79,7 +80,7 @@ public class DataController: NSObject { return } - if context == DataController.viewContext { + if context.concurrencyType == .mainQueueConcurrencyType { log.warning("Writing to view context, this should be avoided.") }