iOS API calls do not expose Particle Cloud variables or functions

Following the code in the reference documentation for the iOS SDK, I am unable to read variables or functions from the Particle Cloud. Running the code below does not list any of the variables or functions on the Photon which are exposed to Particle Cloud.

    func loginWithCredentials(userName:String, userPassword:String) {
        ParticleCloud.sharedInstance().login(withUser: userName, password: userPassword) { (error:Error?) -> Void in
            if let _ = error {
                // catch login failure
                print("Wrong credentials or no internet connectivity, please try again")
            } else {
                print("Logged in")
                var myPhoton : ParticleDevice?
                ParticleCloud.sharedInstance().getDevices {(devices:[ParticleDevice]?, error:Error?) -> Void in
                    if let _ = error {
                        print("Check your internet connectivity")
                    } else {
                        if let d = devices {
                            for device in d {
                                if device.name == "photon" {
                                 myPhoton = device
                                
                                }
                            }
                        }
                        //print the contents of myPhoton
                        print(myPhoton!)
                    }
                }
            }
        }
    }

Here is the printed output:

Logged in
<ParticleDevice 0x282f10410, type: Photon, id: 2c001f000a47363339343638, name: photon, connected: true, flashing: false, variables: {
}, functions: (
), version: 1.4.0, requires update: false, last app: (null), last heard: 2019-10-15 09:06:15 +0000, notes: (null), networkId: (null), networkRole: 0, networkRoleState: 0>

What am I doing wrong?

What do you get when running particle list photon?

particle list photon
photon [2c001f000a47363339343638] (Photon) is online 
 variables:
    analogvalue (int32)
    temp (double)
    mess (string)
    mess2 (string)
  Functions:
    int toggle(String args) 

CLI seems to display the variables and functions correctly. I attempted to call the functions and check the variables anyway in the iOS app. For some reason, the API calls requesting the values of the particular variables work, but you cannot print the variables and functions associated with the device when you print the contents of the object.

@Raimis, would you mind taking a look into this? Thank you for your experienced eye! :slight_smile:

This is odd. Can you please see if our Tinker app sees variables and functions for this device?

The Tinker app works just fine. It is indeed strange that I cannot get the API to list the variables and functions. If you’re interested, I can try making a sample project so you can have a closer look.

Ah my bad!.. getDevices() only loads cached device info - if I remember correctly it contains everything but variables and functions. Call refresh() on device instance you want to inspect. Pseudo-code for that would be:

ParticleCloud.sharedInstance().login {
    ParticleCloud.sharedInstance().getDevices {
        for device in devices {
            if (device.name == "photon") {
                device.refresh {
                    //here you will have variables and functions
                }
            }
        }
    }
}

We do not cache (at least for now) variables and functions, so to use them we need to reach out to device. If that would happen as part of getDevices(), we would waste too much data for cellular devices. Hope this helps!

2 Likes

Great catch! It makes a lot of sense now. Thank you very much.

1 Like