This has been asked many times, but I'm still at a crossroads.
My application is required to save data, this data can be accessed through many areas of the application.
I wanted to avoid core data as it is overkill for what I need to save 3-4 simple different classes.
So I’m using NSCoding, and its working without issue.
Im not stuck on whats the “best” way to access this stored data.
Essentially, singleton or not. Been reading a variety of posts and most say avoid the use of a singleton.
This issue became apparent as my current design uses a singlelton object to provide a way for me to access my data from disc easily. Problem is testing… couldn’t find a way to overcome the inability to test - this is when I decided to ask should i not be using this pattern.
So, what is the better way to access my data - to allow read and write from anyplace within my application.
Use the singleton and just don’t test - or create a new instance, and ask that instance to retrieve my data. I also don’t want to pass data along - I want to be able to easily access data - read and write (using tab bar controller etc)
This was my singleton implementation
class InvalidClientCollection {
static var errorList = InvalidClientCollection.loadErrataClients()
static private let kErrataClientSaveFileName = "errorClientsFile"
static var clientCount:Int {
get {
return errorList.count
}
}
static func loadErrataClients() -> [Client] {
let mysavefile = FilePath(fileName: kErrataClientSaveFileName).filePath
if let data = NSKeyedUnarchiver.unarchiveObjectWithFile(mysavefile) as? [Client] {
return data
}
return [Client]()
}
static func saveErrataClientList() {
let mysavefile = FilePath(fileName: kErrataClientSaveFileName).filePath
NSKeyedArchiver.archiveRootObject(errorList, toFile: mysavefile)
}
}
Should I redesign, so to get my data I would
let mycolleection = InvalidClientCollection()
let mystuff = mycollection.errorList
I guess something like this ?
class InvalidClientCollection {
var errorList:[Client] {
get {
return loadErrataClients()
}
set {
self.errorList = newValue
// maybe even save at same time
//saveErrataClientList()
}
}
// etc...
}
Any ideas - improved code would be much appreciated.
Aucun commentaire:
Enregistrer un commentaire