Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3953

URLConnection Implementation Best Practice

$
0
0

Hi, I am trying to create a class to connect to a rest service. I basically need to connect to a rest api for a db. Since the service i am using does not have a SDK for Xojo, i am trying to create one for me for Mac and Window and maybe to share it with others.

As i am creating this class, i have some questions on how to implement it since i will need a network connection to the service.

My first idea was to make a class for my db and then instantiate a URLConnection (sync) anytime i had to connect to the db service. This i believe it is not the best practice since i read the xojo docs and they mentioned that using sync can “Freeze” my app. Also this does not work well for responses since i can’t get status,error or things like that. At least what i have tried.

The second way of doing this was to drag a URLConnection class and modify it to my needs but then I will need to write an if statement (possibly long) for each of the responses that i get back in the ContentReceived event. I noticed that by doing this, i run into some issues with sticky headers and content. Also i need to drag this object to my window to use it. Not a bad idea but i want to have a better design for implementation.

The third way of doing this is to create a class for my db service. Then in any method of my db class where i need a network connection to the service, i can create an instance of the URLConnection. At this point this implementation is not bad, the only problem is that I now have to use the addHandler and the addressOf to pass a custom method that i have created in the window where i am using the db class instance so i can get the response or the error. This is not a good design because i feel this is very coupled.

The last version is to create the db class, use a URLConnection instance and then pass somehow (not sure how to do this) a function (for ContentReceived) to any method where i need to use the URLConnection. This way the function can be pass to the addHandler and addressOf and at least is a little decoupled.

Sorry for the question but i see many tutorials teaching you how to use the URLConnection and most of them just drag the object to the window where they need it. The problem is that not all use cases are that simple. If you have any examples (real life) in github i can see that will be awesome. Thanks for all your time and support.

Let me know what you think. I am eager to learn what is the best practice to do this. By the way, i could be describing something wrong in the above comment but i am writing this with my limited understanding of OOP (hence i am tyring to get better with OOP).

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 3953

Trending Articles