Ideal way to pull data (JSON) from a service using Monotouch and iOS?

I have an iPhone app that pulls almost all of the data from ASP.NET MVC services.

It basically just returns JSON.

When I run the application in the simulator, the data is reset very quickly, etc. However, when I use the actual device (3G or WiFi), it is very slow. To such an extent that the application crashes too long.

a) Should I not call the service from the FinishedLaunching method in AppDelegate?

b) Am I calling the service incorrectly?

The method I use looks something like this:

public static JsonValue GetJsonFromURL(string url) { var request = (HttpWebRequest)WebRequest.Create (url); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using(var response = (HttpWebResponse)request.GetResponse()) { using(var streamReader = new StreamReader(response.GetResponseStream())) { return JsonValue.Load(streamReader); } } } 

Is there a better or faster way that I should access the service? I read about how to do something in different threads or make asynchronous calls so as not to block the user interface, but I'm not sure what the best approach or how this code will work.

+4
source share
1 answer

a) Should I not call the service from the FinishedLaunching method in AppDelegate?

You get a limited time to run your application, i.e. return from FinishedLaunching , or the iOS watchdog will kill your application. This is approximately 17 seconds (but may vary between device / iOS versions).

Anything that takes some time is best done in another thread running with FinishedLaunching . This is even more important if you use network services because you cannot be sure how long (or even if) you will receive a response.

b) Am I calling the service incorrectly?

It looks good. However, remember that the simulator has faster access to the network (most likely), much more RAM and CPU power. A large dataset can take a lot of memory / processor time to decode.

Starting from another thread will at least take extra time. It can be as simple as adding the code (below) inside your FinishedLaunching .

 ThreadPool.QueueUserWorkItem (delegate { window.BeginInvokeOnMainThread (delegate { // run your code }); }); 

You can see how Touch.Unit does this by looking at TouchRunner.cs .

Note: you can test without using (requesting) compressed data, since time / memory for unpacking may not be very useful on devices (compared to the simulator). Actual testing must be confirmed;)

+4
source

Source: https://habr.com/ru/post/1379663/


All Articles