iOS and Network management

iOS and Network management

The intelligent management of network accesses allows an application to help conserve the battery. If no network connection is available, access to the network should be prevented until the connection is restored.

The main problem unrelated to the programmer is the quality of the signal being received. This is because with a low signal the transmission is slower and the use time is extended. It is also convenient to avoid heavy operations such as video streaming. Unless you are using a WiFi connection because this type of network consumes far less energy technologies 4G LTE, 3G… This is because the LTE devices use Multi-Input and Multi-Output (MIMO English Multiple-Input and Multiple-Output), which allows the concurrent use of signals and allows you to keep two channels of the LTE connection simultaneously. Similarly, all network antennas need to scan for repeater tower signals to connect to. 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 searches for WiFi nodes to which it can connect.

The use of memory storage cache* it will avoid downloading 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 if there is 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 we have a code example of how one could efficiently check the status of the network. This example uses the library Reachability.swift[6].

Figure 1: Example of network accessibility with Reachability.swift

1. A call is made to the method in charge of performing the network operation, it receives as a parameter a dictionary with the content to be sent in the operation.

2. To find out the status of the network connection, a call is made to the method isReachable(), this will return whether it is accessible or not.

3. A new object of the type is created Reachability.

4. Using the object that was created in step 3, its property is accessed isReachable that will offer us the necessary information to know if the network is accessible or not. If it is accessible the method will return a result true, otherwise 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 kind of error, in case of error an error message will also be received that can be shown to the user.

To monitor the network and the execution of a queue of operations when the network is available. The libraries have been used Reachability.swift and Alamofire[7] (Figure 2)

1. Class NetworkOperation it has a property called isAPIReachable which is used to check if the network is reachable or not.

2. Property reachability and networkOperationQueue they are private, since you do not want them to be accessible from outside

2. 1. reachability. It provides us with information about the accessibility of the network.

2. 2. networkOperationQueue. It’s an operations queue. This queue will only allow the execution of one operation at a time.

3. Of NotificationCenter we get when the accessibility status changes and enable or disable the operations queue.

4. Method performNetworkOperation generates and updates the list of operations.

5. To create and glue the operations the method is used enqueueRequest.

Figure 2: Example network monitoring and execution of 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 to avoid bombarding the server with multiple and simultaneous requests. A plausible solution is to use two different queues, one for obtaining heavy images (non-critical data for the application) and another with which the most necessary information will be obtained.

It is also a good practice to turn on or off this operation queue depending on the need of the application. A queue with tasks that are no longer needed will need to be canceled, in order to prevent their future execution.

Development Outsourcing | Unreal Outsourcing

Ready to see us in action:

More To Explore
Enable registration in settings - general
Have any project in mind?

Contact us: