0

I'm building an app that sends (every x seconds) to an API the location values with some extra values (session, ID, etc) that is working nice (see here Update CLLocation Manager on another method). But for improved feature, we are considering the device can lost (for some amount of time) internet connection. So I need to temporary store all values and when reconnected send them again to the API.

I'm considering several ways to do it:

  • Core Data (difficult implementation)
  • Realm (very little experience)
  • NSDisctionary

Can anyone suggest (and show how, if possible) the best way to implement this feature?

Community
  • 1
  • 1
ggirao
  • 13
  • 5

2 Answers2

0

If you want to store a some of non-sensitive values (such as a password), I suggest to use NSUserDefaults, you can easily use it like a dictionary:

Note: Swift 2 Code.

For example:

    // shared instance (singleton)
    let userDefaults = NSUserDefaults.standardUserDefaults()

    // **storing**:
    let myString = "my string"
    userDefaults.setObject(myString, forKey: "kMyString")

    let myInt = 101
    userDefaults.setInteger(myInt, forKey: "kMyInt")

    let myBool = true
    userDefaults.setBool(myBool, forKey: "kMyBool")

    userDefaults.synchronize()

    // **retrieving**:
    //use optional binding for objects to check if it's nil
    if let myRetrievedString = userDefaults.objectForKey("kMyString") as? String {
        print(myRetrievedString)
    } else {
        // if it's nil
        print("there is now value for key: kMyString")
    }

    // if there is no value for key: kMyInt, the output should be zero by default
    let myRetrievedInt = userDefaults.integerForKey("kMyInt")

    // if there is no value for key: kMyBool, the output should be false by default
    let myRetrievedBool = userDefaults.boolForKey("kMyBool")
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • TY @Ahmad. That was my first choice and thinking. But we can be using this app for sea navigation (not sure about how long can a trip take, could take hours!). Wouldn't be that to much for store it? – ggirao Oct 06 '16 at 21:39
  • [check this](http://stackoverflow.com/questions/7510123/is-there-any-limit-in-storing-values-in-nsuserdefaults) I hope the answer helped :) – Ahmad F Oct 06 '16 at 21:42
  • You are the man! TY. I will give it a try and put answered if ok! – ggirao Oct 06 '16 at 21:48
  • BTW a little extra help; How should I increment values (of the key(s))? The values are always updating, and I need to build a route/course. Not sure if I explained myself well – ggirao Oct 06 '16 at 21:51
  • well, if I understand it right, you don't have to generate a new key for a new value, instead, add all of them in an array -for example- and set the array as a value for the same key each time, setting an object on the same key replaces the new value. Hope that's what you are asking about – Ahmad F Oct 06 '16 at 21:54
  • That's it!So the structure will be something like this: array([lat => 123, lon => 456, timeStamp => 01:00], [lat => 789, lon => 444, timeStamp => 02:00]) and so on – ggirao Oct 06 '16 at 22:00
  • in this case you should use a dictionary, or store each value on separated keys. Note: I recommend to use CLLocation to store lat and lng in one value – Ahmad F Oct 06 '16 at 22:11
  • That's what I asked in first time :-) You are right in your note about the lat and lon. – ggirao Oct 06 '16 at 22:14
0

Tadaaaaaa:

func arrayOfDictionaries() {
        var offline:[[String:AnyObject]] = []
        offline.append(["LATITUDE: ": userLocation.coordinate.latitude, "LONGITUDE: ": userLocation.coordinate.longitude, "SPEED: ": userLocation.speed])

        NSUserDefaults().setObject(offline, forKey: "offLine")

        if let offLinePositions = NSUserDefaults().arrayForKey("offLine") as? [[String:AnyObject]] {
            //print(offLinePositions)  


            for item in offLinePositions {
                print(item["LATITUDE: "]! as! NSNumber)  // A, B
                print(item["LONGITUDE: "]! as! NSNumber)  // 19.99, 4.99
                print(item["SPEED: "]! as! NSNumber)  // 1, 2
            }
        }

    }
ggirao
  • 13
  • 5