0

Pretty simple question - how to store Images, Strings and Doubles temporally in Swift?

My app gets ID from server, using this ID app can request from server to download image and then with another request can get String and Double. Then app has to show image with corresponding String and Double. All downloaded data should be deleted once app is killed and downloaded again once app starts.

Does using custom class would be a good idea?

For example, custome class:

class UserInformation: NSObject {
var string: String
var double: Double
var image: String

init(string: String, double: Double, image: String) {
    self.string = string
    self.double = double
    self.image = image
    }
}

Then to save and access data:

var info = [UserInformation]()

func save() {
     //Alamofire functions to download image and string, double values goes here

     var newImage = //Downloaded image from server
     var newString = //Downloaded String from server
     var newDouble = //Downloaded Double from server

     //Save image
     let imageName = NSUUID().UUIDString
     let imagePath = getDocumentsDirectory().stringByAppendingPathComponent(imageName)
     if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
         jpegData.writeToFile(imagePath, atomically: true)
     }

     let userInfo = UserInformation(string: newString, double: newDouble, image: imageName)
     info.append(userInfo)
 }

     func load() {
     //For ex. I am using [0], although in my app
     //it would be for _ in _ loop to access all existing values
     let info = UserInformation[0]
     string.text = info.string
     double.double = info.double
     let path = getDocumentsDirectory().stringByAppendingPathComponent(info.image)
     imageView.image = UIImage(contentsOfFile: path)
}

func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

EDIT I realize that using NSUserData (as in my example) is possible way to store data, what I am unsure if that is a good idea, in my case

Xernox
  • 1,706
  • 1
  • 23
  • 37
  • Yeah, possible, although I am not sure what to use. I have read that NSUserDefaults are not good storing data over 100Kb? – Xernox Dec 10 '15 at 13:07
  • I found two alternatives save photos to Documents or Photo Library, both of them are not what I am looking for because photos will be used while app is running and usually not used again once app is closed. – Xernox Dec 10 '15 at 13:23

2 Answers2

1

What do you want is named "Persistent Data". You can achieved that through many methods: 1. You can use third-party library 2. Save data in plist file or in CoreData or in UserDefaults. You can create a dictionary that contain anything you want, exceptions are UI Objects like UIImage. You can convert UIImage in NSData and after that save NSData.

The easiest way is to save in NSUserDefaults. An example how to do that is there: Save images in NSUserDefaults?

Community
  • 1
  • 1
Cosmin
  • 676
  • 7
  • 15
  • NSUserDefaults may be the easiest, that's why I though about it, but I have read that it is not a good idea to store data over 100Kb in NSUserDefaults? – Xernox Dec 10 '15 at 13:15
0

I personally adore Haneke and AwesomeCache. Both have been written in Swift.

Note about data persistence:

iOS has many persistent data storage solutions, eg:

etc.

But I suspect that you actually don't want to store anything for longer time period. I think that what your are trying to implement is already in default iOS behavior. And depending on how complex your data is you can:

  • use above examples (in case your data is very complex) - you can store all your data and wipe it when app launches.
  • refactor to singleton pattern your UserInformation. This will make your UserInformation instance persistent till your app gets killed.

After quick look on your code the second option is better in your case.

zuziaka
  • 575
  • 3
  • 10
  • I am new at programming, so just to be clear, you would recommend storing all data (Images/Strings/Doubles) in cache? – Xernox Dec 10 '15 at 13:14