I have a class (Patient) with many instances.. I need a way to share the instances with other users, so I thought of converting the instance into a string and sharing it, then reconverting it into an instance
From instance to string
func ToString(pt: Patient) -> String {
let myarray: [String] = [
pt.id,
pt.name,
String(pt.age),
pt.address,
pt.tele,
pt.notes,
dateToString(pt.date),
pt.history,
pt.diagnosis,
(pt.investigations.map {$0.name + "{|}" + $0.value}).joinWithSeparator("{|X|}"),
(pt.treatments.map {$0.name + "{|}" + $0.dose}).joinWithSeparator("{|X|}"),
(pt.images.map {$0.imagePath + "{|}" + $0.caption}).joinWithSeparator("{|X|}")
]
return myarray.joinWithSeparator("{|NEWLINE|}")
}
From string to instance
func FromString(mystring: String) {
let pt = Patient()
let ptarray = mystring.componentsSeparatedByString("{|NEWLINE|}")
let ixarray = ptarray[9].componentsSeparatedByString("{|X|}")
let rxarray = ptarray[10].componentsSeparatedByString("{|X|}")
let photoarray = ptarray[11].componentsSeparatedByString("{|X|}")
let ixcarray = ixarray.map { Investigation(name: $0.componentsSeparatedByString("{|}")[0], value: $0.componentsSeparatedByString("{|}")[1]) }
let rxcarray = rxarray.map { Treatment(name: $0.componentsSeparatedByString("{|}")[0], dose: $0.componentsSeparatedByString("{|}")[1]) }
let photocarray = photoarray.map { Pic(imagePath: $0.componentsSeparatedByString("{|}")[0], caption: $0.componentsSeparatedByString("{|}")[1]) }
pt.id = ptarray[0]
pt.name = ptarray[1]
pt.age = Int(ptarray[2]) ?? -1
pt.address = ptarray[3]
pt.tele = ptarray[4]
pt.notes = ptarray[5]
pt.date = stringToDate(ptarray[6])
pt.history = ptarray[7]
pt.diagnosis = ptarray[8]
pt.savePatient()
_ = ixcarray.map { x in pt.addInvestigation(x) }
_ = rxcarray.map { x in pt.addTreatment(x) }
_ = photocarray.map { x in pt.addImage(x) }
}
Notes:
investigation, treatment, and images are array of classes with two properties
I used "|" as a separator because I disabled it in my app so the user can't interfere with my functions
I use Realm in my app for data persistance
My specific concern is performance issues because thousands of instances may be shared at once