Method Issue in makeWebRequest

I know this has to be a silly error... A foreground makeWebRequest (if I'm running a CIQ 5 device). This is in a module (not a class). I first tried to setup the callback and got "Symbol 'method' not Found". This forum showed that I need to instantiate it using "new Lang.Method()". Now I'm getting past the makeWebRequest but getting a Symbol Not Found error without any context on line number or the offending symbol. Any ideas?

  • To me it sounds like the problem is with the place of the callback function. The code we see is in a module? I guess the callback is in global scope, that's why you pass the $? I'm not sure if this is supposed to work, but what I would try to do is to move both the call to makeWebRequest and the callback function to the same class, and try to make it work that way. After (hopefully) you make it work, then you can try to move them bit by bit and try to figure out either what is the right syntax or where is the bug in the compiler and report that.

  • Yes those are in a module. I found reference code from Travis in this forum suggesting for a global module, to do this. I'll play around and get this working. Thanks.

  • I posted this exact question here yesterday but it wasn't yet approved.

    I fixed it by passing the callback in the following way:

    class DataFetcher {
        function fetchData(url as String, callback as (Method(responseCode as Number, data as Dictionary or String or Null) as Void)) as Void {
            Comm.makeWebRequest(
                url,
                {},
                { :method => Comm.HTTP_REQUEST_METHOD_GET },
                callback // No need for `new Lang.Method(...)`, just pass the reference directly
            );
        }
    }

    And in another class:

    class MainDelegate {
        function onSelect() {
            var fetcher = new DataFetcher();
            fetcher.fetchData("https://example.com/api", method(:handleResponse));
        }
    
        function handleResponse(responseCode as Number, data as Dictionary or String or Null) as Void { // Important that the method signature is defined like this
            System.println("Response received: " + responseCode);
        }
    }

    This prevents the symbol related error AND compilation errors by ensuring the proper method type signatures are set.

    Hope this help!

  • Ok I don't get it. But the "$" failed and "self" worked. mentioned in another thread that these should be identical. Oh well, it works now!

    var responseCallback = new Lang.Method($, :on_signature_response);

    var responseCallback = new Lang.Method(self, :on_signature_response);

  • I don't know in which context it was written that $ and self are identical, but to me it looks (even before you tried it) that they are different.

    $ is the outermost context, self is the innermost context, when there are single or multiple nested classes. I would guess the only circumstance they are identical is when you're in a global context. I don't use modules, so maybe that adds some complications I am not aware of.

  • $ is the outermost context, self is the innermost context, when there are single or multiple nested classes.

    I know what you're trying to say here, but I don't think this is the best choice of words.

    I like the way Garmin documents "$" and "self":

    https://developer.garmin.com/connect-iq/monkey-c/objects-and-memory/

    "Within a method implementation you can refer to your current instance using either the self or me keywords." (Here, "instance" refers to a class instance of course.)"

    "Sometimes you want to run your search from the global namespace instead of your current scope. You can do this using the bling symbol $. The bling symbol refers to global scope:"

    Words like "innermost" might lead to an incorrect interpretation in a situation like this:

    import Toybox.Lang;
    //...
    
    
    var foo = 42; // global variable at "$" scope
    
    
    class SomeClass {
      var foo = 24; // member variable
      
      var someMethod() {
        var foo; // local variable
        
        var x = self.foo; // refers to member variable on current instance
        // not the "innermost foo", which is arguably the local variable
        var y = foo; // refers to local variable
        var z = $.foo; // refers to global variable
      }
    }

    TL;DR

    - self, me = current instance (like this in other languages)

    - $ = global scope