smalltalkpharopharo-5

How can I refresh a JSON file for a certain period in pharo


Basically I connected to a server with pharo using identification. Then I used Znclient to get to myserver/json file which contains a collection of key and value. How can I refresh this Json file every 40 sec without running out of memory and How can I iterate over it to collect a specific key?

This is what I did so far

"                         Login                          "
"********************************************************"
|a data|
a := ZnClient new.
a get: 'https://MyServer'.
a
headerAt: 'referer' put: 'MyServer';
formAt: 'email' add: 'myEmail';
formAt: 'password' add: 'myPassword'.

a post.
a get: 'MyServer/json'.

"                   get Json file      "
"*******************************************************
data := NeoJSONReader fromString: a contents

Solution

  • You can create a loop that does the work and waits 40 seconds:

    process := [ [ self shouldStillRun ] whileTrue: [ 
          self fetchDataAndDoWork.
          40 seconds asDelay wait. ] ]
       forkAt: Processor userBackgroundPriority
       named: '<processName>'.
    

    Above I assume that shouldStillRun and fetchDataAndDoWork are methods in a class containing these code. If you want to play with this code in the Playground replace them with some custom snippets of code. For example:

    shouldStillRun := true.
    process := [ [ shouldStillRun ] whileTrue: [ 
          | data |
          '<create the client>'
          data := NeoJSONReader fromString: a contents.
          40 seconds asDelay wait. ] ]
       forkAt: Processor userBackgroundPriority
       named: '<processName>'.
    

    As long as you do not store all the data return by each call you should not have a memory problem.

    If your data represents a dictionary then NeoJSON will return a dictionary object, and you can just use the at: message to get the value. You can inspect the data object to see what you get back.