Skip to content

Adding Count operation to Context #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ extension NSManagedObjectContext: Context {
return typedResults
}

public func count<T: Entity>(_ request: FetchRequest<T>) throws -> Int {
guard let entity = T.self as? NSManagedObject.Type else { throw StorageError.invalidType }
let fetchRequest: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity.entityName)
fetchRequest.predicate = request.predicate
fetchRequest.fetchOffset = request.fetchOffset
fetchRequest.fetchLimit = request.fetchLimit
let result = try self.count(for: fetchRequest)
return result
}

public func insert<T: Entity>(_ entity: T) throws {}

public func new<T: Entity>() throws -> T {
Expand Down
1 change: 1 addition & 0 deletions SugarRecord/Source/Foundation/Protocols/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
public protocol Context: Requestable {

func fetch<T: Entity>(_ request: FetchRequest<T>) throws -> [T]
func count<T: Entity>(_ request: FetchRequest<T>) throws -> Int
func insert<T: Entity>(_ entity: T) throws
func new<T: Entity>() throws -> T
func create<T: Entity>() throws -> T
Expand Down