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

Commit

Permalink
Add write async completion block
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi Parpinel committed Jan 30, 2019
1 parent 6f5dd5c commit 3173eda
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ extension ChildCoreDataStore: ChildDataStore {
}

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

writeAsync(writeBlock, completion: nil)
}

func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void, completion: (() -> Void)?) {
precondition(isSetup, "You must setup the data store before trying to write to it")

let accessor = readWriteAccessor()
accessor.context.perform {
writeBlock(accessor)
accessor.save()
completion?()
}
}

Expand Down
10 changes: 9 additions & 1 deletion JustPersist/Classes/Core/Interface/DataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public protocol DataStore: class {
- parameter writeBlock: Block that contains the work that will be done. The block will be passed a ```DataStoreReadWriteAccessor``` which can be used to access information in the data store that can then be modified. Once this block has finished running, the accessor will ensure that any changes that took place within the block will be persisted. It is **not** safe to perform UI work within this block.
*/
func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void)


/**
Write to the data store asyncronously

- parameter writeBlock: Block that contains the work that will be done. The block will be passed a ```DataStoreReadWriteAccessor``` which can be used to access information in the data store that can then be modified. Once this block has finished running, the accessor will ensure that any changes that took place within the block will be persisted. It is **not** safe to perform UI work within this block.
- parameter completion: Block that is executed when the changes are saved.
*/
func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void, completion: (() -> Void)?)

// MARK: Child Data Store

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ extension MagicalRecordDataStore: DataStore {
}

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

writeAsync(writeBlock, completion: nil)
}

public func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void, completion: (() -> Void)?) {
precondition(isSetup, "You must setup the data store before trying to write to it")

let accessor = readWriteAccessor
Expand All @@ -150,7 +153,8 @@ extension MagicalRecordDataStore: DataStore {
accessor.context.mr_saveToPersistentStoreAndWait() // Saves context and parent contexts all the way up to the persistent store.
}
}



public func makeChildDataStore() -> ChildDataStore {
return ChildCoreDataStore(parentDataStore: self, parentContext: readAccessor.context, errorHandler: errorHandler)
}
Expand Down
8 changes: 6 additions & 2 deletions JustPersist/Classes/Wrappers/Skopelos/SkopelosDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ extension SkopelosDataStore: DataStore {
}
}

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

public func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void, completion: (() -> Void)?) {
precondition(isSetup, "You must setup the data store before trying to write to it")

skopelos.writeAsync { context in
Expand All @@ -131,8 +130,13 @@ extension SkopelosDataStore: DataStore {
}
}
writeBlock(writeAccessor)
completion?()
}
}

public func writeAsync(_ writeBlock: @escaping (DataStoreReadWriteAccessor) -> Void) {
writeAsync(writeBlock, completion: nil)
}

public func makeChildDataStore() -> ChildDataStore {
var mainContext: NSManagedObjectContext! = nil
Expand Down

0 comments on commit 3173eda

Please sign in to comment.