Skip to content

Commit

Permalink
Added find or create
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed Jun 3, 2023
1 parent a4b440d commit f5507b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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.10'
s.version = '1.4.11'
s.summary = 'A very lightweight Core Data framework.'
# s.resources = 'KangarooStore/Assets/*'
s.swift_version = '5.0'
Expand Down
11 changes: 10 additions & 1 deletion Sources/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension KangarooStore {
}

@available(iOS 15.0, *)
public func execute() async -> [Entity] {
public func executeAwait() async -> [Entity] {
let context = self.context
let fetchRequest = self.fetchRequest
let result = try? await context.perform {
Expand Down Expand Up @@ -140,6 +140,15 @@ extension KangarooStore.Query {
guard let existingEntity = filtered(using: predicate).first() else { return create() }
return existingEntity
}

@available(iOS 15.0, *)
public func findOrCreate(where block: () -> NSPredicate) async -> Entity {
let entities = await filtered(using: block()).executeAwait()
guard let existingEntity = entities.first else {
return create()
}
return existingEntity
}
}

extension KangarooStore.Query {
Expand Down

0 comments on commit f5507b0

Please sign in to comment.