Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #1309: Don't use NSBatchDeleteRequest on In-Memory store(#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
danishjafri88 authored and jhreis committed Aug 10, 2019
1 parent c890dfa commit 96b151f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Data/models/CRUDProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions Data/models/DataController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.")
}

Expand Down

0 comments on commit 96b151f

Please sign in to comment.