Closed maxkhrichtchatyi closed 8 years ago
Try wrapping test_http callback in setTimeout
setTimeout(test_http, 0);
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.
Ok, does it work if you call this function directly?
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)
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
You're right man! I set both the calls with timeout 0 and now it works! Thank you.
added to the docs: http://docs.pushwoosh.com/docs/appcelerator-titanium
Undefined is not an object (evaluating 'Ti.Network.createHTTPClient')