Good afternoon, hrobrozhiteli!
Now all mobile applications (with very rare exceptions) use the network: for authorization, receiving / sending data, etc.
I decided to collect my experience on this topic in the article.
Working with a network in a standard application comes down to solving several tasks:
- authorization
- request and send data
- data storage
- work with pictures
Authorization
All social services have OAuth authentication with facebook and a number of other social networks.
By logging in through them, you get a token, which is sent along with the user id to your server and the application receives the session id from it. It should be borne in mind that both keys may become obsolete at any time. If the session is lost, automatic re-authentication should go to the server. If the token is lost, then the user should be shown an authorization window, after receiving a new token, you need to organize a request to the server to receive the new session id.
If any of the application requests returned with an error “session is outdated” (or a similar one), the authorization method should automatically work out, and then execute the method on which the error fell.
Navigating the application screens
With a large number of different tabs in the application (messages, friends, profiles, check-ins, user activity, news, likes), each new controller requires loading data. There are also background requests to the server that request data on the timer all the time the application is running (these can be requests for the number of new messages, invite to friends, changes in the currency balance within the application, etc.). Also, controllers need to be notified of updates synchronously or asynchronously, depending on the type of request.
Getting pictures from the server




As you can see the picture can be used the same. If the server sends the url only to a large-resolution image, then it is necessary, after loading, to cache, compress, round or round the corners. All that is required for the design of the application.
We save the original locally and when requesting images, we search among cached files. If the original is, but the resolution is needed less or of another form: reduce the image and save the result with the name of the form filename_resolution If there is, show, no, we will load.
')
Data storage
Online store catalog, chat history, news feed. All this is obsolete and changing data volumes of a fairly large amount. Optimal for each such action is to do the server method. Opened the list of messages - requested the first 50 positions (records, messages, products), asynchronously loaded avatars or product photos. Moved to chat-requested the last 50 messages of the history with this user, while scrolling, they asked for another 50.
This will reduce the amount of transmitted data and the application will run quickly on the edge.
Basic architecture solution
The main solution is to organize a class that manages requests. Singleton class (or set of classes) responsible for working with the network.
His tasks:
- Create a queue of calls, automatically arrange it depending on the correct / incorrect server responses
- Send notifications about receiving data to controllers subscribed to certain methods (in ios via nsnotification)
- Send jobs to the image download manager
- Manage the background download stream of user activities (notification of new messages, likes, etc.)
In iOS, the class was implemented based on the standard NSConnection.
Requests are added to the queue. Whether this will be a dynamic priority array or NSOperationQue (as in iOS) is up to you. Each request in the queue takes precedence and if an error is returned with a lost authorization on the last request, the generated priority request is given to the reconnect. This will avoid the situation when the user clicks on the button, the session is lost and nothing happens when the connection error returns.
That's all for now. In the second part I plan to give examples of the code of the connection class, the organization of interaction with the certification and the image manager.
Thank.