Synchronous & Asynchronous Loading of data/images coupled with Multi-Threading (PART 1 – Conceptual)

 

How to load images / data synchronously and asynchronously using multi-threading

While designing applications we have to consider not just availability of resources but also their timely delivery. Sometimes an image may be available instantly as it resides on the user’s system (let’s say it is in the main bundle of your iOS application).

In other situations, we may have to fetch it from a remote server using its URL. Your application may not be able to use it immediately; as external factors are at play, such as size of the image (perhaps the image is dynamic,) the network speed etc. Sometimes data could reside on multiple servers and we may like the application to perform a task only when all the data from different sources are collated.

All the above situations bring us to a decision point where we have to take a call on synchronous vs. asynchronous loading. A lot of times we also have to use multi-threading to avoid a bottleneck.

Before I explain and demonstrate how to load data/images synchronously and asynchronously, coupled with multi-threading for iphone or ipad, let us first understand these three important terms, viz. Synchronous, Asynchronous, and Multi-threading.

Allow me to present a scenario that will establish conceptual clarity with respect to these terms and then we see their application.

Another Day at Office? Perhaps Not!

It is another regular Friday at office and you have just settled down to work, after your daily meeting with your boss. You have a series of tasks to accomplish. You have to send some emails, read office memos, order lunch for afternoon, and so on. More importantly your day also involves working on a report that has to be delivered to the boss by Saturday evening, for a client meet scheduled for the coming Monday. You plan on doing half the report today, and for the other half you intend come into the office for a few hours on Saturday morning.

So far, everything is going smooth. You text your sweetheart and tell her that you are looking forward to your date in the evening , and continue with your work. After some time, you notice that she has texted back with a message that she is equally excited about meeting you, but you read between the lines and realize that her message also contains a mild reminder that you shouldn’t be late. You smile at the phone, and continue to work.  So far the day promises to be beautiful.

But in almost all offices, a beautiful calm that encourages you to hum a tune, often has disaster clinging to its tail. Almost as if by design, the moment you start to feel good about the day, your colleague walks into your cubicle and drops the bombshell. The client has advanced the meeting with your boss to tomorrow morning.

As the news sinks in, you realized that you have no other option than to finish the report today instead of tomorrow afternoon, and submit it to the boss today. Now you are in a terrible fix. What must you do? It was just last week that you had stood her up for a date, and it was only after a lot of cajoling from your end that things had normalized between the two of you.

What should you do?

You are at the crossroads, at an important decision point.

Because you are hassled you do the first thing that comes to your mind.

Decision A:

You send a text message to your girl friend explaining the situation, open the report and attack it ferociously. A few minutes into work, and your phone begins to ring. You instinctively know that it’s your girlfriend calling. You also know that taking the call would distract you from doing your work, but you also know that ignoring the call will land you in a bigger mess. You find yourself faced with Hobson’s choice, which is taking the call.

So you gingerly pick up the phone and take the call. Expectedly and quite understandably, she’s furious. You know that telling her that you need to get back to your report will further exacerbate the situation and might push your relationship over the cliff. It takes you 15 minutes of persuasion and a promise to make it up to her during the weekend. Then you return to the report, drained of energy, you find it difficult to concentrate. You didn’t just lose those 15 minutes but also sacrificed your efficiency. Now it appears that you’ll have to stay the whole night to complete the report.

Not a happy situation to be in, right?

Now let us say that you aren’t the regular joe, but a budding CEO with foresight, coupled with an ability to understand and persuade others.

Decision B:

The first thought that strikes you after your colleague informs you that the meeting has been rescheduled to tomorrow, is to inform your girl friend. But you are able to project it all into the future and see that she’s not going to take it well and that you’ll have to lose precious time in the process.

So you decide against texting her. Instead you use your skills of persuasion to convince your friend you help you complete the report. You also tell him that whenever he is in need of any help from you, you’ll be there for him. When he accepts, you appreciate his help and display your appreciation by giving an offer of a sumptuous treat to him during the weekend. Now he is more than willing to help you, and you have your evening free to spend with your sweetheart.

So, which decision was a better one? A or B?  While you figure it out, let us understand the terms Asynchronous, Synchronous and Multi-threading in the context of the above story.

Synchronous Tasks:

Your typical workday involves performing certain tasks. Once you arrive in office you started doing various tasks like sending emails, ordering lunch, reading office memos etc. All this is part of your daily work and you do without affecting any of your tasks. Your work continues as you wait for the replies of the emails that you sent in the morning or for the lunch that you had ordered in the morning and are expecting around noon. Even your task of sending the SMS to your girl friend about your date did not affect your work which continued.

You perform various tasks during the day. Completion of one task does not impact the beginning of another.  All these tasks can be termed as Asynchronous tasks  because you do them as and when you have to do them, and they do not impact your pace of work.

Asynchronous Tasks:

Now let us understand Asynchronous tasks.

After your girlfriend responded to your SMS about the cancellation of your date, your work had to wait till the call was over. Though you eventually did a good job of explaining the precarious situation you were in, it took you 15 minutes to convince her to not be upset and angry with you. For this duration, your entire work had halted and you knew that your subsequent work- performance depended heavily on the outcome of the call.

Now this task of receiving the call from your girl friend can be called a synchronous task. While you were performing the delicate task of explaining your GF about your inability to keep up with the date, your execution of  your work could continue only after the call ended.

Multi-threading :

Multi-threading can be best explained in the light of the altered scenario.

When you learned that the meeting was advanced, you didn’t rush to message your girl friend. Instead you donned your thinking hat. Earlier you had used your skills of persuasion to persuade your girl friend, now you use them on your friend, and request him to bail you out. He realizes the importance of true love in one’s life and agrees to help you by completing your report by evening. In the meantime you complete your other important routine tasks. In both scenarios your report gets completed.  However in the second scenario, your friend completed “your” report.

In programming parlance, you decided to open another thread (don’t you and your dear friend share a bond, a “thread” of friendship?) to do your work. The preparation of the report (another task) and your remaining tasks for the day progressed simultaneously the whole day, and by the evening all your daily tasks were over. You simply resorted to do multi-threading. As a budding CEO a quick cost-benefit analysis will tell you that even though you had to promise your friend a treat, you benefitted through this multi-threading.

The key point to note here is that multi-threading is not a problem as long as you have resources available.

It’s clear that the second scenario that resulted from Decision B was the best outcome for everyone – for you, your girl friend, your boss, client and also your friend who is now eagerly waiting for his weekend treat.

From the above discussion, it is easy to deduce that when taking decision about Asynchronous, Synchronous, Multi-threading tasking we require assessment of the following:

1. The context of the current situation

2. Cost of alternate resources

3. Severity of the different tasks affecting the system

4. Availability of threads (read friends who are willing to pitch in and help.

And, of course, you have to don the THINKING HAT!

 

To Be Continued… Application of this Concept in iPhone/iPad Application Development in PART 2 (the next post).

— §§§ —

 

iPhone iPad games & apps development in Delhi NCR Noida Gurgaon | iOS trainings for corporates & companies india | iOS app development trainings in Noida Gurgaon Delhi NCR | iPhone iPad based game development in Gurgaon Noida Delhi NCR India | iOS trainings courses workshops in swift programming in India at Delhi NCR Noida Gurgaon | iPhone development trainings & classes in swift & objective C | iPhone & iPad games for eLearning | mobile apps development india | iPhone app and game development courses workshops in swift | iOS apps and games development courses & trainings in Delhi NCR Noida Gurgaon India | Xcode iOS trainings and courses in swift for corporates & companies | games and apps developer & producer india | iOS games and applications development Delhi NCR Noida Gurgaon India | Indian iOS Games and apps developers | ios application development in swift objective C India | iPhone & iPad Application development in India | Apple development training courses Delhi NCR Gurgaon Noida