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

Commit

Permalink
CPD-7965: Assume single private scratch context is used for all the w…
Browse files Browse the repository at this point in the history
…riting operations
  • Loading branch information
albertodebortoli committed Nov 22, 2017
1 parent b9162ae commit 61529bf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 48 deletions.
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ PODS:
- MagicalRecord (~> 2.3.2)
- JustPersist/Skopelos (1.0.2):
- JustPersist/Core
- Skopelos (~> 2.0.0)
- Skopelos (~> 2.1.0)
- MagicalRecord (2.3.2):
- MagicalRecord/Core (= 2.3.2)
- MagicalRecord/Core (2.3.2)
- Skopelos (2.0.1)
- Skopelos (2.1.0)

DEPENDENCIES:
- JustPersist/MagicalRecord (from `../`)
Expand All @@ -20,9 +20,9 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
JustPersist: 487ad6f8086102801a2ae93290c62d0b6cc11501
JustPersist: 5f460f093f7fd0b4a6be26de248e018787f1fd8c
MagicalRecord: 53bed74b4323b930992a725be713e53b37d19755
Skopelos: 25d8ffe8d5a0dbc4499ef3e7aabc08ecfa236bbc
Skopelos: 27608e9ed6e880d6a33c1cbd569cf3ed1c6c8f06

PODFILE CHECKSUM: b99881e9785c7f31dda9fd2f24bc7c8a01141fce

Expand Down
2 changes: 1 addition & 1 deletion JustPersist.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ At it's core, JustPersist is a persistence layer with a clear and simple interfa

s.subspec 'Skopelos' do |ss|
ss.dependency 'JustPersist/Core'
ss.dependency 'Skopelos', '~> 2.0.0'
ss.dependency 'Skopelos', '~> 2.1.0'
ss.source_files = 'JustPersist/Classes/Wrappers/Skopelos/*'
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ open class MagicalRecordDataStore: NSObject {
fileprivate var setupBlock: () -> Void
fileprivate var tearDownBlock: () -> Void
fileprivate var isSetup = false
fileprivate var isExecutingWriting = false

open static func stack(_ dataModelFileName: NSString, securityApplicationGroupIdentifier: NSString, errorHandler: DataStoreErrorHandler? = nil) -> MagicalRecordDataStore {

Expand Down Expand Up @@ -77,13 +76,13 @@ open class MagicalRecordDataStore: NSObject {

fileprivate var readAccessor: CoreDataAccessor!

fileprivate func readWriteAccessor() -> CoreDataAccessor {
lazy fileprivate var readWriteAccessor: CoreDataAccessor = {

let privateContext = NSManagedObjectContext.mr_context(withParent: NSManagedObjectContext.mr_default())
privateContext.shouldDeleteInaccessibleFaults = false
let accessor = CoreDataAccessor(withContext: privateContext)

if let errorHandler = errorHandler {
if let errorHandler = self.errorHandler {

accessor.errorHandler = { error in
DispatchQueue.main.async {
Expand All @@ -92,7 +91,7 @@ open class MagicalRecordDataStore: NSObject {
}
}
return accessor
}
}()
}

// MARK: - DataStore
Expand Down Expand Up @@ -134,40 +133,22 @@ extension MagicalRecordDataStore: DataStore {

precondition(isSetup, "You must setup the data store before trying to write to it")

if isExecutingWriting {
// stop here
print("YOLO! Already a writing happening in this data store")
}

isExecutingWriting = true

let accessor = readWriteAccessor()
let accessor = readWriteAccessor
accessor.context.performAndWait {
writeBlock(accessor)
accessor.context.mr_saveToPersistentStoreAndWait() // Saves context and parent contexts all the way up to the persistent store.
}

isExecutingWriting = false
}

public func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void) {

precondition(isSetup, "You must setup the data store before trying to write to it")

if isExecutingWriting {
// stop here
print("YOLO! Already a writing happening in this data store")
}

isExecutingWriting = true

let accessor = readWriteAccessor()
let accessor = readWriteAccessor
accessor.context.perform {
writeBlock(accessor)
accessor.context.mr_saveToPersistentStoreAndWait() // Saves context and parent contexts all the way up to the persistent store.
}

isExecutingWriting = false
}

public func makeChildDataStore() -> ChildDataStore {
Expand Down
19 changes: 0 additions & 19 deletions JustPersist/Classes/Wrappers/Skopelos/SkopelosDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ open class SkopelosDataStore: NSObject {
fileprivate var securityApplicationGroupIdentifier: String?
fileprivate var storeType: StoreType
fileprivate var isSetup = false
fileprivate var isExecutingWriting = false
open var errorHandler: DataStoreErrorHandler?

public init(sqliteStack modelURL: URL, securityApplicationGroupIdentifier: String?, errorHandler: DataStoreErrorHandler? = nil) {
Expand Down Expand Up @@ -98,13 +97,6 @@ extension SkopelosDataStore: DataStore {

precondition(isSetup, "You must setup the data store before trying to write to it")

if isExecutingWriting {
// stop here
print("YOLO! Already a writing happening in this data store")
}

isExecutingWriting = true

_ = skopelos.writeSync { context in
let writeAccessor = CoreDataAccessor(withContext: context)

Expand All @@ -118,21 +110,12 @@ extension SkopelosDataStore: DataStore {
}
writeBlock(writeAccessor)
}

isExecutingWriting = false
}

public func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void) {

precondition(isSetup, "You must setup the data store before trying to write to it")

if isExecutingWriting {
// stop here
print("YOLO! Already a writing happening in this data store")
}

isExecutingWriting = true

skopelos.writeAsync { context in
let writeAccessor = CoreDataAccessor(withContext: context)

Expand All @@ -146,8 +129,6 @@ extension SkopelosDataStore: DataStore {
}
writeBlock(writeAccessor)
}

isExecutingWriting = false
}

public func makeChildDataStore() -> ChildDataStore {
Expand Down

0 comments on commit 61529bf

Please sign in to comment.