Skip to content

Commit

Permalink
Add async support
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed Jun 3, 2023
1 parent aee53c4 commit a4b440d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions KangarooStore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KangarooStore'
s.version = '1.4.9'
s.version = '1.4.10'
s.summary = 'A very lightweight Core Data framework.'
# s.resources = 'KangarooStore/Assets/*'
s.swift_version = '5.0'
Expand All @@ -21,7 +21,7 @@ A very lightweight Core Data framework. You\'ll be able to fetch object in an ea
s.author = { 'Carlos Duclos' => 'darkzeratul64@gmail.com' }
s.source = { :git => 'https://github.com/DarkySwift/KangarooStore.git', :tag => s.version.to_s }

s.ios.deployment_target = '13.0'
s.ios.deployment_target = '14.0'
s.watchos.deployment_target = '6.0'
s.source_files = 'Sources/**/*'

Expand Down
15 changes: 4 additions & 11 deletions Sources/KangarooStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,10 @@ open class KangarooStore {
}
}

public func save(in type: ContextType, block: @escaping (ManagedObjectContext) throws -> Void) async throws {
try await withCheckedThrowingContinuation { continuation in
saveAsync(in: type, block: block) { result in
switch result {
case .success:
continuation.resume(with: .success(()))
case .error(let error):
continuation.resume(with: .failure(error))
}
}
@available(iOS 15.0, *)
public func save(context: ManagedObjectContext, block: @escaping () throws -> Void) async throws {
try await context.perform {
try block()
}
}

Expand All @@ -139,7 +133,6 @@ open class KangarooStore {

public func saveSync(in type: ContextType,
block: @escaping (ManagedObjectContext) throws -> Void) {

switch type {
case .view:
saveViewContext(mode: .sync, block: block)
Expand Down
12 changes: 10 additions & 2 deletions Sources/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ extension KangarooStore {
}
}
}

@available(iOS 15.0, *)
public func execute() async -> [Entity] {
let context = self.context
let fetchRequest = self.fetchRequest
let result = try? await context.perform {
try context.fetch(fetchRequest.toRaw(in: context)) as [Entity]
}
return result ?? []
}
}
}

Expand All @@ -112,8 +122,6 @@ extension KangarooStore.Query {
return ManagedObject(entity: entity, insertInto: context) as! Entity
}



public func findOrCreateFirst() -> Entity {
guard let existingEntity = first() else { return create() }
return existingEntity
Expand Down

0 comments on commit a4b440d

Please sign in to comment.