Dictionary access by index

I get a dictionary with a list of IDs and values like:

dict = {
  GUID1=> {...},
  GUID2=> {...},
  GUID3=> {...},
}

The list is sorted by datetime (a field inside the {...}.
I need to read only the last 10 entries. So I tried to get the key array with dict.keys( ) and loop over it. But this key list is not sorted like the dictionary. The keys are GUIDs and that's why  I can't sort by it.
And the dictionary I can only access by key, not by index - or is it possible?

Has anyone an example how to access the dictionary by index - or if it's possible to get the key array in same order like the dictionary.

Many thanks.

  • glance view has much less memory

  • I built a basic sort algorithm. The steps are now:

    - get dictionary keys dict.key()

    - create empty target array

    - loop at keys

    - read dict entry by current key, build new smaller dict with only necessary fields (date only for test) to insert uin the target array.

    - loop at target array comparing the date string. At found insert position, split the array with .slice into 2 parts. Insert is not possible!

    - overwrite taget array with array.addAll() adding the 2 slices and the new entry between with array.add().

    That's working so far. But with currently 58 source dictionary entries, I get the watchdog at half way (number 28).

    So manipulation arrays is a torture without simple functions like sort or insert.
    I don't believe that I will get it to run on a real device if the simulator is already crashing.

  • This sounds like insertion sort, which is pretty inefficient (O(n^2) worst case).

    Have you tried this?

    - Loop through dict.values() array and construct a new array where each element is an array with only two members: the dateCreated field, and the index to the original array. (Arrays are expensive, but dictionaries are even more expensive). Alternatively you could save the memory overhead of all those nested arrays by using two flat arrays, but then you'd have to tailor your sort algorithm to work with the 2 arrays simultaneously.

    - Sort the new array with an efficient algorithm like quick sort

    - Use the sorted new array to grab the elements you need from the dict.values()

    Given that your source data is fairly small, I'm not sure whether this will be enough of an improvement.

    If that doesn't work, do you have control over the source data (so you can tailor the response to be filtered and sorted)? If not, you can write a proxy service to grab the data and manipulate it, using a cloud app service (which may be free or cheap depending on your usage.)

  • I figured out how to quote \o/

    If that doesn't work, do you have control over the source data (so you can tailor the response to be filtered and sorted)? If not, you can write a proxy service to grab the data and manipulate it, using a cloud app service (which may be free or cheap depending on your usage.)

    I was thinking about this as well. It might work fine and dandy for a personal app that no-one else uses. But if you are going to capture traffic from/to a watch owner, you are probably also capturing the tokens between them and the source data provider. Which might not be something you would want. I would like to know these things as a user. 

  • It really depend on the app and how the proxy is used.  I know of one app that uses a proxy with close to 1.5 million downloads.  It would likely break if each user did a download every minute, but that's not the case.  Also, when you collect and send any user data, your app must have a privacy policy about what data is collected and how it's used. (the GDPR stuff)

  • I'm not even talking about GDPR. I'm talking API keys and tokens that are send across the wire. The proxy terminates the connection, this can read everything and do things as you, the watch owner. I would be very skeptical of such things from unknown third parties.

  • Are you sending any user specific data to a unknown server?  That's an issue with or without a proxy

  • I'm assuming here that the user is sending data to a known data source, which now gets intercepted by a third party. Why would a user send data to an unknown server? 

  • Your app to your sever for example...  That server would be unknown.

    What kind of data are you sending?  I'd trust something like Azure far more than your own server, as I know Azure goes by the rules.

  • I'm not following you. Where does the "my server" come from?