Created
November 14, 2019 17:53
-
-
Save AndresR173/aef5f4526684830fb5ae73a50412de7d to your computer and use it in GitHub Desktop.
CoreData helper based on generics implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CoreDataHelper.swift | |
// | |
// Created by Andres Rojas on 14/11/19. | |
// Copyright © 2019 Andres Rojas. All rights reserved. | |
// | |
// MIT License | |
// | |
// Copyright (c) 2019 Andres Rojas | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
import CoreData | |
import Foundation | |
class CoreDataHelper { | |
static let shared = CoreDataHelper() | |
private var context: NSManagedObjectContext? | |
func setContext( _ context: NSManagedObjectContext?) { | |
self.context = context | |
} | |
///Get all values from an entity | |
static func getAll<T: NSManagedObject>( predicate: NSPredicate? = nil, sortDescriptor: [NSSortDescriptor]? = nil, completion: @escaping(([T]?) -> Void)) { | |
guard let context = CoreDataHelper.shared.context else { | |
completion(nil) | |
return | |
} | |
context.performAndWait { | |
do { | |
let request: NSFetchRequest<T> = T.fetchRequest() as! NSFetchRequest<T> | |
request.predicate = predicate | |
request.sortDescriptors = sortDescriptor | |
let values = try context.fetch(request) | |
completion(values) | |
} catch { | |
completion(nil) | |
} | |
} | |
} | |
///Remove object | |
static func deleteObject(_ object: NSManagedObject, completion: @escaping ((Bool) -> Void)) { | |
guard let context = CoreDataHelper.shared.context else { | |
completion(false) | |
return | |
} | |
let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) | |
privateMOC.parent = context | |
privateMOC.perform { | |
do { | |
let managedObject = privateMOC.object(with: object.objectID) | |
privateMOC.delete(managedObject) | |
try privateMOC.save() | |
context.performAndWait { | |
do { | |
try context.save() | |
completion(true) | |
} catch { | |
completion(true) | |
} | |
} | |
} catch { | |
completion(true) | |
} | |
} | |
} | |
///Get main context Attachment | |
static func getMainContextObject<T: NSManagedObject>(_ entity: T) -> T? { | |
var managedObject: T? | |
guard let context = CoreDataHelper.shared.context else { return nil } | |
context.performAndWait { | |
managedObject = context.object(with: entity.objectID) as? T | |
} | |
return managedObject | |
} | |
///Get main context Attachment | |
static func getMainContextObjectBy<T: NSManagedObject>(predicate: NSPredicate) -> T? { | |
var managedObject: T? | |
guard let context = CoreDataHelper.shared.context else { return nil } | |
context.performAndWait { | |
do { | |
let request: NSFetchRequest<T> = T.fetchRequest() as! NSFetchRequest<T> | |
request.predicate = predicate | |
managedObject = try context.fetch(request).first | |
} catch { | |
} | |
} | |
return managedObject | |
} | |
///Purge Entity | |
static func purge<T: NSManagedObject>(entity: T.Type, completion: @escaping((Bool) -> Void)) { | |
guard let context = CoreDataHelper.shared.context else { return } | |
context.performAndWait { | |
do { | |
let request: NSFetchRequest<NSFetchRequestResult> = T.fetchRequest() | |
guard let objects = try context.fetch(request) as? [T] else { | |
completion(false) | |
return | |
} | |
objects.forEach { | |
context.delete($0) | |
} | |
try context.save() | |
completion(true) | |
} catch { | |
completion(false) | |
} | |
} | |
} | |
///Get main context Attachment | |
static func getMainContextObjects<T: NSManagedObject>(predicate: NSPredicate? = nil, sortDescriptors: [NSSortDescriptor]? = nil) -> [T]? { | |
var managedObject: [T]? | |
guard let context = CoreDataHelper.shared.context else { return nil } | |
context.performAndWait { | |
do { | |
let request: NSFetchRequest<T> = T.fetchRequest() as! NSFetchRequest<T> | |
request.predicate = predicate | |
request.sortDescriptors = sortDescriptors | |
managedObject = try context.fetch(request) | |
} catch { | |
} | |
} | |
return managedObject | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment