[Resolved] Need help adding activity indicator to a network call

Sometimes the network call takes long enough to where the user might think the app is stuck so I want to add an indicator, telling the user to that processing is indeed in progress.

This is my network call:

function writexml(xmlsend,destip,destport)  
 client = socket.connect(destip, tonumber(destport))  
 if client then  
 client:send(xmlsend .. "\0")  
 s = client:receive()  
 client:close()  
 result = s  
 else  
 result = "ERROR: Connection Timed Out"  
 else  
 result = "No Connection"  
 end  
 return result  
end  

Have tried native.setActivityIndicator() and also setting an image alpha to 1.0 before the writexml function is called and in the writexml function itself.

It seems no matter what or where I’m trying to turn on some kind of activity indicator the socket gets done before anything displays.

What am I missing where?
I’d be quite happy to simply make an image visible before the socket.connection and turn it off when socket is closed. [import]uid: 6547 topic_id: 23833 reply_id: 323833[/import]

Have you tried putting it in the very first line of the function and simply removing it immediately if no connection? [import]uid: 52491 topic_id: 23833 reply_id: 96033[/import]

Yup, even tried to reload the image first, inside the function:

 processingImage2 = display.newImage( "processing.png")  
 processingImage2.x = display.contentWidth/2  
 processingImage2.y = display.contentHeight/2  
 processingImage2.alpha = 1.0  

The image shows up after socket call has completed, not before.

Have also tried to set native.setActivityIndicator( true ) as first line in and last line before the function is called.

The whole process starts via a button press and it seems that nothing UI related gets executed until after the socket call.

I.e. I can’t set the button to alpha=0 as soon as it’s pressed. That is to say I can set it but it doesn’t disappear until after the socket call.

I’m going to try and redo it all into a single function as a test I.e. put it all into the onButtonPress.

Edit:
Same results in single function. Only way I can get the image and/or ActivityIndicator to appear is to comment out the socket call, otherwise they only appear after the socket all has completed.
[import]uid: 6547 topic_id: 23833 reply_id: 96038[/import]

Would it be possible to display the activity indicator and then do the socket call on a timer after a few milliseconds, perhaps? I wonder if that might be a suitable workaround. [import]uid: 52491 topic_id: 23833 reply_id: 96048[/import]

Thanks for the suggestion Peach. Will try that. Having a bit of trouble wrapping my head around the example in the API section. Checking to see if other examples will make it a bit easier to grasp since I have to pass the parameters.

The plan would be to wrap the whole writexml call into the delay, correct?

EDIT:
I think this will work. Still sorting out passing of parameters, etc. but I think on the right track. Thank You. [import]uid: 6547 topic_id: 23833 reply_id: 96100[/import]

Correct.

I do hope this works, please update the thread when you get the chance to give it a run - would be very happy to hear it had helped get around the issue :slight_smile:

Peach [import]uid: 52491 topic_id: 23833 reply_id: 96242[/import]

Finally had a chance to actually install on device via iTunes as a final test and the scheme seems to work ok. Still have to do more extensive testing, Ie. put it users’ hands, but so far it looks like an easy work-around. [import]uid: 6547 topic_id: 23833 reply_id: 97550[/import]

I am very happy to hear that, I hope it proves to be solid when you let users play with it as well.

Thanks for the update- tentatively marking as resolved :wink: [import]uid: 52491 topic_id: 23833 reply_id: 97642[/import]