Intelligent management of network access allows an application to help battery conservation. If no network connection is available, access to the network should be prevented until the connection is re-established.
The main problem Foreign to the programmer is the quality of the signal that is received. This is because with little signal transmission is slower and extends the time of use. It is also convenient to avoid heavy operations such as video streaming. Unless you are using a WiFi connection since this type of network consumes much less energy than 4G LTE, 3G technologies … this is because LTE devices use multiple-Input and Multiple-Output (MIMO) services which allows the concurrent use of signals and allows you to maintain two LTE connection channels simultaneously. Similarly, all Network antennas need to scan for repeat tower signals to which they can connect. Another factor of this type that makes intense use of these technologies, specifically WiFi, occurs when it is activated but is not linked to any compatible point, since it continuously performs searches for WiFi nodes to be able to connect.
Using in-memory storage cache* it will prevent the download of components that will be used continuously and will not necessarily be updated in the short term.
To finish knowing this technology, the following should be taken into account:
1. Check for an appropriate network connection before performing any network operations.
2. Continuously monitor the availability of the network and act appropriately according to its status.
Below is a code example of how you could efficiently check the network status. This example uses the library Reachability.swift.
Figure 1: example of network accessibility with Reachability.swift
1. A call is made to the method in charge of performing the network operation, this receives as a parameter a dictionary with the content that you want to send in the operation.
2. To know the status of the network connection, a call is made to the method isReachable(), this will return whether it is accessible or not.
3. The new object of type is created Reachability.
4. The object created in Step 3 accesses its property isReachable that will provide us with the necessary information to know if the network is accessible or not. If accessible the method will return a result trueotherwise the result will be false.
5. With the result of isReachable the operation will be sent or not. In both cases a result will be returned to the call made in Step 1.
6. The result received will be the confirmation that everything has gone well or there was some type of error, in case of error you will also receive an error message that can be shown to the user.
To monitor the network and the execution of an operations queue when the network is available. Libraries have been used Reachability.swift and Alu-fire (Figure 2)
1. Class NetworkOperation you have a property called isAPIReachable which is used to check if the network is accessible or not.
2. Property reachability and networkoperation they are private, as you do not want them to be accessible from outside
2. 1. reachability. It gives us information about the accessibility of the network.
2. 2. networkoperation. It’s an operations queue. This queue will only allow one operation to be executed at the same time.
3. Of Notification centre we get when the accessibility state changes and enables or disables the operations queue.
4. Method performNetworkOperation generates and updates the list of operations.
5. To create and paste operations the method is used enqueueRequest.
Figure 2: example network monitoring and running a network operations queue
The OperationQueue they do not stop or suspend the execution of any operation. A suspended queue simply means that the previous operation will not be executed until the current one finishes its execution.
Using queue-based network requests is the best way not to bombard the server with multiple and simultaneous requests. A plausible solution is to use two different queues, one for obtaining heavy images (non-application critical data) and another with which the most necessary information will be obtained.
It is also a good practice to turn this operation queue on or off depending on the need of the application. A queue with tasks that are no longer needed should be to cancel them, to avoid their future execution.