Reputation: 263
I'm using CoreData in my app. There're 4 tables in the database: User, Medicine, Schedule and Patient. For each table I need to write an Add function to add data into the database like this:
func addUser(email: String, password: NSData, handler: @escaping (_ status: SaveStatus, _ error: NSError?) -> Void){
let managedContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: DB.TABLE.USER, in: managedContext!)!
let nurse = NSManagedObject(entity: entity, insertInto: managedContext) as! User
nurse.email = email
nurse.password = password
save { (status, error) in
handler(status, error)
}
}
func addMedicine(name: String, handler: @escaping(_ status: SaveStatus, _ error: NSError?) -> Void){
let managedContext = (UIApplication.shared.delegate as? AppDelegate)?.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: DB.TABLE.MEDICINE, in: managedContext!)!
let medicine = NSManagedObject(entity: entity, insertInto: managedContext) as! Medicine
medicine.name = name
save { (status, error) in
handler(status, error)
}
}
and so on ...
I want to write an generic function like this:
func addDataToDB<DATA>(object: DATA, handler: @escaping SaveStatusHandler){
switch DATA {
case DATA is LoginData:
break
default:
break
}
}
I don't know what type should the LoginData be, so I can compare the DATA with it. Or is there any better way to do this?
Upvotes: 2
Views: 111
Reputation: 4849
The fastest solution for this case, since each Entity
has a different save
implementation, is to declare a Saveable
protocol
protocol Saveable {
associatedtype Parameters
static var entityName: String { get }
static var context: NSManagedContext { get }
func setup(with parameters: Parameters)
func save(_ handler: () -> ())
}
And then use this protocol as generic parameter in the addDataToDB
function
func addDataToDB<T>(params: T.Parameters, _ handler: () -> ()) where T: Saveable {
let entity = NSEntityDescription.entity(forEntityName: T.entityName, in: T.contenxt)!
let object = NSManagedObject(entity: entity, insertInto: T.context) as! T
object.setup(with: params)
object.save(handler)
}
Parameters
in this case should be a struct
for example, or a dictionary, it's completely up to you
Upvotes: 1