I recently came back to Xojo development after 8 years and already started a mid-sized web app which calls REST API to get data.
I have a few general questions about the usage of URLConnection and hope that some of you can chime in and add their two cents.
As I understand from various forum posts, it is generally recommended that URLConnection instances are only used for a single request. Did I get this right?
When I add handlers to handle Error and ContentReceived events, is it better to use AddressOf or WeakAddressOf? If so, why? For me it is not clear from the documentation.
Is it general practice to call the Disconnect method in the ContentReceived method after retrieving the data? Or will this be done automatically?
Is it guaranteed that all of the body is received when the ContentReceived event is fired? I once had a strange error message ( I believe something like “request still in progress” ) and I’m not sure why that was.
When I build a wrapper class for the REST API that I am calling, is it recommended that the class will be a subclass of URLConnection or do you instantiate a new URLConnection object inside the wrapper class for every call you make?
As a side question, not directly related to URLConnection, when you receive JSON data, do you use JSONItem or the ParseJSON method? Which one is the “newer” or the faster method?
Thank you all a lot for your contributions. I’m really looking forward to do more and more projects in Xojo.
IMHO, WeakAddressOf should never be used for this. They’re really tricky to get right and the URLConnection is kept alive through the ContentReceived event automatically anyway. You should however be keeping a reference if you want to reuse the sockets.
You don’t need to call disconnect unless you need to abort the socket early.
It depends on the server and the response as I recall. ContentReceived could contain a partial response if the server sends the response “chunked” or if the client requests only a piece. Generally for API work you don’t have to worry about this.
That’s up to you. If you want to use it directly like a socket, use a subclass. If you want to change the API drastically or have a socket pool so you don’t need to keep instantiating the classes, put an inside.
Neither is newer now. JSONItem has used ParseJson and GenerateJSON for over a year under the hood. I find JSONItem easier to understand, but that’s just my preference.
They both do the same thing, with the difference being side-effects (for lack of a better term).
AddressOf will retain a reference to the object that hosts the method, so until RemoveHandler is called, that object will never go out of scope. If the URLConnection is a property within the object that hosts the method you are calling, it will be a circular reference and that object’s Destructor can not fire until you call RemoveHandler.
WeakAddressOf does not maintain a reference, so it’s up to you to keep a reference to the object that hosts the method. If you don’t, and your URLConnection tries to use that method, your app will crash.
Because it’s about references, this distinction is only applicable when referencing object methods. If you want to use a class (Shared) method or a module method, you must use AddressOf.
Agreed. I tend to put the RemoveHandler calls in a method and call that method in each of the places that could indicate that the socket needs to go away. If subclassing, you could create a Close method for that for instance.