1

I am trying figure out what would be the best option to save some info into cache. I want it to be in there until notification pops out and button is pressed. I am already done everything else but I do not know what is the best solution for handling temporary values in cache. I need to use them even if app is closed. Is CoreData the best solution with NSUserDefaults? Lets assume that I have an array: var myArray = [] and I have textField. Now I want that texField input to be in array and if notification category button is pressed it deletes the first array item from the myArray. I need all this even if app is closed. Faulty code is inside "**".

Edit: The main problem is that how can delete every time the last value from array? It is not working if app is closed but works if it is opened.

This is what i've done so far. This is whre I call out notification and it's action:

This is my array: var myArray = [String]()

Here I add object into array:

And now if notification is fired and button is pressed I would like to fire this function to delete first object from array:

  • What have you tried? You've got a decent whack of code here; have you tried putting it all together? What *specific* problems do you see? – dcsohl May 31 '16 at 17:22
  • @dcsohl All my code is together. I just splitted it for this question. The main problem is that I need to store some value into cache and I do not know what would be the best way to do it. Is it CoreData with NSMutableArray? –  Jun 01 '16 at 15:19

1 Answers1

4

You can use NSUserDefaults. They are accessable if the app is closed or open. Read more about NSUserDefaults Here

Saving Example:

 NSUserDefaults.standardUserDefaults().setObject(nameField.text!, forKey: "name")

Retrieving Example

Best Practice

if let predicate = NSUserDefaults.standardUserDefaults().objectForKey("name") {
//NSUserDefaults has a value
} else {
 //NSUserDefaults does not has a value
}
  • @EricD. Sorry you told me that before! –  May 31 '16 at 15:49
  • @Alexander so I can do like this? var nimi = NSUserDefaults.standardUserDefaults().setObject(nameField.text!, forKey: "name") let predicate = CNContact.predicateForContactsMatchingName(nimi) –  May 31 '16 at 15:53
  • @MaeseppTarvo i updated my answer hope this helps! –  May 31 '16 at 16:00
  • @MaeseppTarvo if my answer solved your problem kindly mark it as the right answer. Thnx –  Jun 01 '16 at 15:43
  • @Alexander sorry but it aint working if app is closed. I used this way to store array into userdefaults and then retrive it and delete at index(0) But not working if app is closed. Only if it is opened. But it is closest answer yet. You got the right answer tick. –  Jun 02 '16 at 21:46
  • @MaeseppTarvo upload your project on Github and i'll take a look at it! –  Jun 03 '16 at 14:03
  • @Alexander here you go: https://github.com/MaeseppTarvo/TempCo/tree/master/TempCo The code is in the ViewController.swift mainly. –  Jun 03 '16 at 15:52
  • @MaeseppTarvo where are you using the code I provided? –  Jun 03 '16 at 16:14
  • @Alexander Look at this. codeshare.io/7YP3J I didn't use it anymore since it didn't work. But I think am gonna give up on this one since coredata is most complicated thing in Swift –  Jun 03 '16 at 16:32
  • @MaeseppTarvo try this if let predicate = NSUserDefaults.standardUserDefaults().stringForKey("name") { } –  Jun 03 '16 at 16:39
  • @Alexander it deletes only the first entered one. –  Jun 03 '16 at 18:32
  • @Alexander check it out. I explained more in question. I edited it. I do not understand why it deletes only one from the NSUserDefaults? –  Jun 04 '16 at 10:59