Pushwoosh / pushwoosh-appcelerator-titanium

Other
33 stars 16 forks source link

Can't use Ti.Network.createHTTPClient in success callback #6

Closed maxkhrichtchatyi closed 8 years ago

maxkhrichtchatyi commented 8 years ago

Undefined is not an object (evaluating 'Ti.Network.createHTTPClient')

var pushnotifications =  require("com.pushwoosh.module");

function test_http() {
    var httpRegister = Ti.Network.createHTTPClient({
        onload : function() {
            Ti.API.error("Onload");
        },
        onerror : function() {},
        timeout : 5000
    });

    httpRegister.open("GET", "https://google.com");
    httpRegister.send();
}

/* test function */
test_http();

pushnotifications = pushNotificationsRegister({
    "pw_appid":"<ID>",
    success:function(e) {       
        test_http();
    },
    error:function(e) {
        Ti.API.error("Error during registration: " + e.error);
    },
    callback:function(e) {
        Ti.API.info('JS message event: ' + JSON.stringify(e.data));
    }
});
shaders commented 8 years ago

Try wrapping test_http callback in setTimeout

setTimeout(test_http, 0);

fstrazzante commented 8 years ago

I need to do the same thing; I tried this code with setTimeout(test_http, 0); but it doesn't work anyway: I receive the same error.

shaders commented 8 years ago

Ok, does it work if you call this function directly?

fstrazzante commented 8 years ago

Yes it works! I give you an example: in my case I have to store the device token (e.registrationId) in my server and I'm using this code

function test_http(e) {
    var httpRegister = Ti.Network.createHTTPClient({
        onload : function() {
            Ti.API.error("Onload");
        },
        onerror : function() {},
        timeout : 0
    });
    url = "www.serverurl.example/something/e.registrationId";
    httpRegister.open("GET", url);
    httpRegister.send();
}

/* test function */
//test_http(); <- it works

pushnotifications.pushNotificationsRegister({
    "pw_appid": "XXXXX-XXXXX",
    "gcm_projectid": "XXXXXXXXXX",
    success:function(e) {       
        test_http(e);
    },
    error:function(e) {
        Ti.API.error("Error during registration: " + e.error);
    },
    callback:function(e) {
        Ti.API.info('JS message event: ' + JSON.stringify(e.data));
    }
});

the error:

Creating [object TiNetworkHTTPClient] in a different context than the calling function.
[ERROR] :  Script Error {
[ERROR] :      column = 978;
[ERROR] :      line = 1;
[ERROR] :      message = "null is not an object (evaluating 't.open')";
[ERROR] :      sourceURL = "file:///private/var/mobile/Containers/Bundle/Application/............................................";
[ERROR] :      stack = "test_http@file:///private/var/mobile/Containers/Bundle/Application/................................................";
[ERROR] :  }

I tried to print the e.registrationId in test_http(e) and it works, I can see the right value but I can't store on my server online! (i tried to store this value on local app database and I received a similiar error)

shaders commented 8 years ago

Somehow this very simple callback invocation forces Titanium to run callback code on a separate thread (not the main one):

- (void)onDidRegisterForRemoteNotificationsWithDeviceToken:(NSString *)token
{
    NSDictionary *result = [NSDictionary dictionaryWithObject:token forKey:@"registrationId"];

    [self.successCallback call:[NSArray arrayWithObject:result] thisObject:nil];
}

Therefore you cannot access UI objects, and have the same issue with contexts. At the moment I have no idea how to fix this in a plugin (the code is already super simple). However I have reports that this problem usually could be solved using setTimeout function. This forces the callback javascript code to run on correct thread.

Also see: https://developer.appcelerator.com/question/121072/warnings-and-possible-issues

fstrazzante commented 8 years ago

You're right man! I set both the calls with timeout 0 and now it works! Thank you.

shaders commented 8 years ago

added to the docs: http://docs.pushwoosh.com/docs/appcelerator-titanium