Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
spnkr committed Oct 19, 2022
1 parent fe50a65 commit cb046ce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 84 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import CoreDataPlus
// An existing NSManagedObject
public class Drawing: NSManagedObject { }

extension Drawing: CoreDataDeletable,
CoreDataCountable,
CoreDataSearchable,
FindOrCreatableBy {
extension Drawing: ManagedObjectDeletable,
ManagedObjectCountable,
ManagedObjectSearchable,
ManagedObjectFindOrCreateBy {

}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import Foundation
import CoreData
import Foundation

public protocol CoreDataCountable where Self: NSFetchRequestResult {
public protocol ManagedObjectCountable where Self: NSFetchRequestResult {
init(context: NSManagedObjectContext)

static func entity() -> NSEntityDescription
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}

public extension CoreDataCountable {
static func countFor(_ predicate: NSPredicate?, context:NSManagedObjectContext) -> Int {

public extension ManagedObjectCountable {
static func countFor(_ predicate: NSPredicate?, context: NSManagedObjectContext) -> Int {
CoreDataLogger.shared.log("counting \(predicate.debugDescription)")

let request = NSFetchRequest<Self>()
request.predicate = predicate
request.entity = self.entity()
request.entity = entity()

return (try? context.count(for: request)) ?? 0
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
import Foundation
import CoreData
import Foundation


public protocol CoreDataDeletable {
/// Removes all objects from the context, and saves the context.
static func destroyAll(context: NSManagedObjectContext)
public protocol ManagedObjectDeletable {
/// Removes all objects from the context, and saves the context.
static func destroyAll(context: NSManagedObjectContext)
}

public extension CoreDataDeletable {
public extension ManagedObjectDeletable {
static func destroyAll(context: NSManagedObjectContext) {
context.perform {
let request = (self as! NSManagedObject.Type).fetchRequest()
request.entity = (self as! NSManagedObject.Type).entity()
var objects:[Any]
do {
objects = try context.fetch(request)
} catch {
objects = []
}
for obj in objects {
context.delete(obj as! NSManagedObject)
}

var objects: [Any]
do {
objects = try context.fetch(request)
} catch {
objects = []
}
for obj in objects {
context.delete(obj as! NSManagedObject)
}

try! context.save()
}
}

static func destroyAllNoWrap(context: NSManagedObjectContext) {
let request = (self as! NSManagedObject.Type).fetchRequest()
request.entity = (self as! NSManagedObject.Type).entity()
var objects:[Any]

var objects: [Any]
do {
objects = try context.fetch(request)
} catch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,26 @@
import Foundation
import CoreData
import Foundation

public protocol FindButDoNotCreatableById where Self: NSFetchRequestResult {
var id: String { get set }

init(context: NSManagedObjectContext)

static func entity() -> NSEntityDescription
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}

public extension FindButDoNotCreatableById {
static func findButDoNotCreate(id: String, context: NSManagedObjectContext) -> Self? {
let request = NSFetchRequest<Self>()
request.predicate = NSPredicate("id = %@", id)
request.fetchLimit = 1
request.entity = entity()

do {
let objects: [Self] = try context.fetch(request)

if let object = objects.first {
return object
}
} catch {
CoreDataLogger.shared.log("FindButDoNotCreatableById context fetch failure: \(error.localizedDescription)")
}

return nil
}
}

public protocol FindOrCreatableBy where Self: NSFetchRequestResult {
public protocol ManagedObjectFindOrCreateBy where Self: NSFetchRequestResult {
// suggested: use id. add this as a column in your db. reason: you are probably conforming to Identifiable in your NSManagedObject subclass.
// var id: String { get set }

init(context: NSManagedObjectContext)

static func entity() -> NSEntityDescription
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}

public extension FindOrCreatableBy {
public extension ManagedObjectFindOrCreateBy {
static func findOrCreate(id: String, context: NSManagedObjectContext) -> Self {

// TODO: log developer errors as warnings:
// dump(entity().attributesByName["id"]!.type == NSAttributeDescription.AttributeType.string)
// entity().attributesByName["idx"]?.type

findOrCreate(column: "id", value: id, context: context)
}

static func findOrCreate(column: String, value: Any, context: NSManagedObjectContext) -> Self {
let request = NSFetchRequest<Self>()
request.predicate = Predicate("\(column) = %@", value)
Expand All @@ -67,24 +37,22 @@ public extension FindOrCreatableBy {
}

let object = Self(context: context)

if let object = object as? NSManagedObject {
object.setValue(value, forKey: column)
}

return object
}



static func findButDoNotCreate(id: String, context: NSManagedObjectContext) -> Self? {

// TODO: log developer errors as warnings:
// dump(entity().attributesByName["id"]!.type == NSAttributeDescription.AttributeType.string)
// entity().attributesByName["idx"]?.type

findButDoNotCreate(column: "id", value: id, context: context)
}

static func findButDoNotCreate(column: String, value: Any, context: NSManagedObjectContext) -> Self? {
let request = NSFetchRequest<Self>()
request.predicate = Predicate("\(column) = %@", value)
Expand All @@ -98,9 +66,9 @@ public extension FindOrCreatableBy {
return object
}
} catch {
CoreDataLogger.shared.log("FindButDoNotCreatableById context fetch failure: \(error.localizedDescription)")
CoreDataLogger.shared.log("FindButDoNotCreateById context fetch failure: \(error.localizedDescription)")
}

return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import Foundation
import CoreData
import Foundation

public protocol CoreDataSearchable where Self: NSFetchRequestResult {
public protocol ManagedObjectSearchable where Self: NSFetchRequestResult {
init(context: NSManagedObjectContext)

static func entity() -> NSEntityDescription
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}

public extension CoreDataSearchable {
static func searchFor(_ predicate: NSPredicate?, context:NSManagedObjectContext) -> [Self] {
public extension ManagedObjectSearchable {
static func searchFor(_ predicate: NSPredicate?, context: NSManagedObjectContext) -> [Self] {
let request = NSFetchRequest<Self>()
request.predicate = predicate
request.entity = self.entity()
request.entity = entity()
return (try? context.fetch(request)) ?? []
}
}

0 comments on commit cb046ce

Please sign in to comment.