Seven Peaks Insights

Let’s Get Swiftly

Our iOS meetup summary

Lets-get-swiftly-ios-meetup-summary-at-Seven-Peaks-Software-1536x1024

 

Introduction

In this iteration of the iOS Meetup, our experts from Seven Peaks Software will walk you through the Swift programming language, providing you with the most up-to-date tips and tricks for success in iOS development.

Rupendra Limbore, our first speaker who specializes in Tech Lead and has 12 years of experience in developing iOS apps related to finance, medical, and e-commerce domains, will kick off the meetup by discussing Concurrency in Swift.

Concurrency allows programs to handle multiple tasks at the same time. However, writing a concurrent program is not as simple as it appears. Dealing with threads and locks can be time-consuming, making concurrent programming difficult.

His topic will focus on making it as simple and understandable as possible so that any intermediate to advanced Swift developer can apply these concepts to their projects.

The second speaker, Saravudh (Tul) Sinsomros, is a Senior iOS developer with an innovative tech mind and 10+ years of experience developing iOS apps in the finance and e-commerce domains.

In addition, he possesses prior experience as a backend developer, managing databases, and networking systems.

Subjects and Operators in RxSwift will be covered in depth. RxSwift is a well-known library for creating asynchronous and event-driven programs with observable sequences. Subject and operation are fundamentals of RxSwift.

His topic will help you understand the mechanism and behavior of subjects, as well as the main combination operators in RxSwift. It is beneficial to begin by learning the fundamentals.

Finally, Ruttanachai (Pop) Auitragool -A senior iOS developer with 8 years of experience in e-commerce, video streaming, entertainment, and large-scale enterprise applications- will demonstrate how to improve your app’s performance.

He is passionate about simplifying code, streamlining development processes, and providing excellent user experiences.

You will understand the tools, metrics, and paradigms that can help to smooth your development process and contribute to a better user experience for your app. Because app performance is critical for user engagement and retention.

When an app grows in size, more features are added, and the app’s external dependencies grow. It is critical to assess the impact of these changes and to constantly improve the app for better performance.

 

What is Concurrency?

Using candy as an example, demonstrate the two approaches with the goal of reaching a total of six candies in a bowl. The simplest method is to take one piece of candy at a time and drop it into another bowl, which requires six steps.

The second method involves grabbing two pieces of candy at a time and dropping them into a bowl, resulting in only three moves.

What did this represent?

  • The first thing was to decompose the program into minor steps – approach number 1 or 2.
  • The second thing is the ability of panel execution, this single step can be executed parallelly – demonstration of the two different approaches.
  • The third thing is the order of execution. Keep in mind that it does not matter in which order the candy was picked up and dropped into a bowl. The final outcome is that all candies have been transferred from one place to another.

What did this represent?

There are a few advantages of concurrency and the reason we need it.

  • Minimize Response Time – It takes six seconds to take one piece of candy at a time and drop it into another bowl. However, dropping two pieces of candy into a bowl at a time takes less time, resulting in three seconds. So how did it go from six-second to three seconds? This is the minimum response charge with Concurrency.
  • Maximize Throughput – When picking up one candy, it’s considered a single throughput. When picking up two candies, its throughput was two candies.
  • Efficient Resource Utilization – The first approach uses only one hand to move one piece of candy at a time, which does not require you to utilize the resource because the hand was idle, it was just there. The second approach requires the utilization, for the use of both hands in order to move two pieces of candy at the same time.

Solutions

Using candy as an example, demonstrate the two approaches with the goal of reaching a total of six candies in a bowl. The simplest method is to take one piece of candy at a time and drop it into another bowl, which requires six steps.

1) Thread

A base class that gives us the ability to deal with and manipulate the thread available on the operating system. This third class is defined and declared in the foundation framework.

So while using it, we need to import a foundation framework. Thread is available for our iOS 2.0 and onwards.

This is a very basic class and it can be used with an initializer passing prob to it or we can surpass the third class and implement the main method to it.

What is a third priority?

A preference for timely execution of a strategy.

This third priority varies between zero to one, the revolt value is point five. While Apple documentation clearly states that this value is just a guideline.

This value does not make sure that the priority that we have provided will be obeyed by the operating system because the priority of execution of a thread is decided by an operating system on a colonel level.

So, this value whatever we said is the guideline to an operating system to ask a priority for our threat.

Thread Demo

How can we implement a simple set?

A scenario wherein we need to upload an image to the server. The four stages that you must complete are as follows:

  • Fetch an image
  • Resize the image
  • Compress an image
  • Upload the image

When doing so, you must first define four different classes with the responsibility for each state. Now, this course just contains some, it is not implemented for full functionality, just for the general purpose. They implement our functionality but they don’t care about the threads, the currency, or the background processing.

The problem here is that whenever we call these functions directly from our view controller or view model. They will start on the main thread and the main thread is always utilized for UI updates.

Once we start calling these methods, the UI will get blocked. That’s why we need to call this method background today. These causes were implemented without considering the current currency.

What to do?

Manage and implement different classes to handle concurrency. Now, looking at this fair map that has been implemented. It will take care of the action performing process as well as give us a call back from a caller function.

What is a callback?

A callback can be a piece of code that will get carved once the feature or functionality or block of code has been executed. So once your asynchronous task is done, it is your responsibility to call the callback and give the caller the information that you’re done with the task.

Now, the callback may be simply void, it won’t take any details and it can also give us some results that can be utilized in a calorie function. For this, we use a research email.

What does it do? If we are fetching an image, the result will be expected as an image. In case an error occurs, this email helps us keep track of it.

What do we call this method?

This is the implementation, a very complex one as it is called callback hell.

If we have multiple steps in performing our code, we will get to have something with the invitation and a block inside a block which is referred to as callback hell.

Next is a crucial stage, the color thread. Color thread is something on which a function is getting caught. Many people make the mistake of calling a callback from a queue or from a thread on which it is getting executed.

What happens if you are running something in the background and you are calling a callback on the background thread, your collar function might be from a view controller which can still be caught on the background track.

If you are updating any UI in that, your application will crash because you are updating a UI from a background trend. Which should not be the case.

2) Grand Central Dispatch (GCD)

A base class that gives us the ability to deal with and manipulate the thread available on the operating system. This third class is defined and declared in the foundation framework.

There are three types based on creation access:

  • Main Queue – Given by Apple, it always accesses its main thread. So whenever you have to do things related to UI, you have to do it on the main queue.
  • Global Queue – Deals with background test and it is also provided by Apple.
  • Custom Queue – Have to provide a label and a few of the parameters like QOS and other attributes.

There are three types based on creation access:

  • Serial Queue (e.g main queue) – In which the tasks will be executed one by one in a serial order.
  • Concurrent Queue (e.g global queues) – Something in which the tasks will be executed at the same time in parallel.
  • Custom Queue – Have to provide a label and a few of the parameters like QOS and other attributes.

Quality of Service (QoS)

  • User interactive: executed as quickly as possible
  • User-initiated: high priority queue
  • Default: default priority queue
  • Utility: low priority queue, executed with a view to keeping power efficiency as high as possible without sacrificing too much performance
  • Background: lowest priority queue, tasks will be executed with power efficiency as its priority

We have to consider that the background priority or the background QOS will be used when we have a task that might chant for a few minutes to hours. It’s like indexing, we have to gather some major data from our system in storage.

The interesting thing about the GCD and operation queue is that there will always be a callback.

3) OperationQueue

This is more advanced than GCD, how?

It provides you the functions like counseling, it provides dependency handling and it is easier and more object-oriented to implement.

For the operation queue, we have an operating cost. Operation cost is an upside cost. Apple had provided us to contribute classes with the implementation. They have blog operation and invocation operation. We can also surpass the operation cost on our own and implement a method main.

Once all the dependencies are met for an operation, the operation will start executing. This is a simple occurrence for implementation of the operation.

What is the block operation?

It takes the block in the initializer and the other class that we can implement when we have to override the main method. Initialize the operation class, add it to the operation tube and we are done. Operation Queue is available on iOS 2.0 and onwards.

States

When we start an operation there is something called a state of an operation that is defined and available as a seat of the operation changes. The four states are:

  • Ready: It’s prepared to start – The operation is already ready to execute. That means all the dependencies of an operation of resources are available and are ready to be executed.
  • Executing: The task is currently running – Executing states will happen when the actual operation is executed. So, once we are added to the operation queue, you will just start executing.
  • Finished: Once the process is completed – When all the operation has finished all these tasks, they will go into finish states.
  • Canceled: The task canceled – When a programmer cancels the operation by calling a canceled method. It will go into a canceled state.

These are four important steps in an operation queue.

4) Async-Await (Swift 5.5)

Implemented in Swift. It is available from iOS30 and Swift 5.5. The async-await features give us the ability to implement, and structure concurrency (the two keywords). The same keyword can also be implemented with the triumph cash. The rate parameter will be available for any of the methods as usual.

Advantages:

  • Avoid callback hell/pyramid of doom
  • Less number of lines of code
  • Improved readability
  • Guaranteed return result

Meetup-iOS-Concurrency-r4_page-0007


RxSwift: Subjects & Operators

RxSwift is a library for handling asynchronous tasks. There are 3 main components:

  • Observable – response for producing an event and sending it to other components. The event can be – Data, Complete Signal (success), and Complete signal (error).
  • Observer – Block of code for receive event from Observable and you can do some business logic here
  • Operator – You can add Operator between Observable and Observer to manipulate the event data from Observable before passing it to Observer

Subjects

An observable that can add items at runtime

  • PublishSubjects – It is commonly used in Rx’s application because it is straightforward: when it receives a new item, it will directly emit the item to the subscriber.
  • BehaviorSubjects – The difference between this and PublishSubject is that you will receive an item immediately after subscribing; it will be the most recent item in the Subject.
  • ReplaySubjects – Comes with a buffer, which will keep all items emitted and send them all to the subscriber when subscribing.
  • AsyncSubjects – The simplest subject in RxSwift, emitting only one last item to the subscriber after the subject receives a complete event.

Combining Operators

Operators that work with multiple source Observables to create a single output Observable:

  • CombineLatest – They combine an item from several inputs together and will emit it as a pair to output.
  • Merge – This operator will merge all of the items in sources to output observable.
  • Zip – This is quite similar to CombineLastest, but Zip won’t emit an item that was emitted.
 
RxSubjectAndOperators
 

How to get better performance out of your application

Overview

An approach to application performance optimization – Knowing where to look for performance, improvements and what step to take first is something to consider in order to improve your application.

Modern applications are getting more complex and contain a huge amount of data which we communicate with the external systems, and it can become an engineering nightmare.

When creating a new application, the performance should never be overlooked. The larger and more complex the application is, the sooner you should begin concentrating on performance optimization because the performance of any app is crucial for user engagement and retention.

Continuing to optimize your application performance will make your user use your app more often, and stay more engaged with your application for a longer period of time.

Performance optimization, improve your app performance using MetricKit (a tool and SDK provided by Apple).

There are 8 different performance metrics:

Battery usage
    1. Launch time
    2. Hang rate
    3. Memory
    4. Disk writes
    5. Scrolling
    6. Terminations
    7. MXSignposts
    8. – Process these metrics at your server
    9. – Measure custom internals in the app
    10. – Import MetricKit
    11. – Subscribe to MXMetricManager
    12. – Implement the MXMetricManagerSubscriber method to receive consolidated data

Apple automatically connects these metrics, no code changes are required in your project. Apple will gather the user usage data from the device, console it and send it to the Apple server (iOS13 and iOS14).

However, in the earliest model iOS15, it sends almost immediately. This data analyzes the server and consolidated data is presented in the export.

See how the Xcode Organizer displays the most important metrics from your app.

The MetricKit has been available since iOS13, but now it is iOS15 with a lot of improvements. Also, take a look at our article on iOS 16.

Diagnose & Eliminate Hangs

When the app is not responsive to the user’s input, it can cause the user to leave the application which can lead to a bad user experience. By eliminating hang, your app can be more smooth, quick, and better responsive.

  • Track down hangs and delays in your app
  • Show tools and methods to discover hangs and their causes
  • Explore best practices for eliminating hangs

Check out our live recorded webinar of our iOS Meetup on the Seven Peaks Software Facebook page.

 
 
How-to-Get-Better-Performance-Out-of-Your-App
 
 
Are you a talented iOS developer?
See all our Senior, Mid-level, and Junior iOS developer jobs below!
See iOS Developer Jobs!