Code Quality & Performance with Xamarin || Code Quality & Performance Virtual Conference
13K views
Nov 9, 2023
This session is focused on optimizing performance that involves more than just implementing efficient code. The user’s experience of application performance must also be considered. There are many techniques for increasing the performance and perceived performance, of Xamarin.Forms applications. Collectively these techniques can greatly reduce the amount of work being performed by a CPU, and the amount of memory consumed by an application. Conference Website: https://globaltechconferences.com/event/code-quality-performance-virtual-conference-2021/ C# Corner - Community of Software and Data Developers: https://www.c-sharpcorner.com C# Live - Dev Streaming Destination: https://csharp.live #codequality #Xamarin #virtualconference
View Video Transcript
0:00
My name is Luis Matos and I'm System Engineer, Summary certified. I have an agency. I do a lot
0:13
of talk talking about Summary, Azure and all of those things. If you want to see content about
0:23
summary you can follow me on my social media twitter and linkedin i have a lot of content
0:31
there and also i'm sharing content and sharing a lot of content there today we are going to talk
0:42
about code quality and performance for summary and summary forms but first i need to tell you that
0:50
I don't have enough of time to show you all the things that I have here so what
0:59
that means that we are joints we are just I'm going to show you all the tips
1:07
that you need to follow in order to have code quality and performance in your
1:12
application but we don't have enough time to detail although though all of
1:16
of those tips but I'm going to write a post a blog post but I don't know if it's going to be
1:25
this weekend or it's going to be the next weekend because I'm really busy this weekend but you are
1:31
going to have it so if you want to read that post or you need something follow me on social media
1:38
you will have the content there that's an extra content because I'm going to share all the resource
1:46
that I'm going to use here you will see it. Okay. What I have as a talk I have a recopilation
2:02
of all the best practice tools, techniques and so on that you need for having a performance
2:15
Xamarin application. If you are new in this world, let's start talking about Xamarin
2:24
Xamarin is an open source platform for building modern and performing applications for iOS
2:33
Android, and Windows with .NET. Xamarin is an abstraction layer here that manage communication of shared code with the underlying platform code, can I say
2:52
But when we talk about summary, we have two approaches that we can follow. One of them
2:59
is summary traditional or summary classic as some people call it. With this approach
3:05
can write code, logic code, once and share it across your summoning Android, summoning
3:14
iOS, Windows application or project. And those separate projects, you are writing code that
3:27
is specific for that platform and you have access to the native API
3:39
That means that anything you can do with, I don't know, Object C, Swift, Java, you can
3:49
do it with C-Chart and Xamarin. On the other hand, we have Xamarin.Forms. Xamarin.Forms is a UI framework that allows developers
4:08
to build Samaritan iOS, Samaritan Android, and UWP application from a single share code base
4:20
We have different approaches. We need to know that before we start
4:25
So, let's start. We have the performance, the base, what you need to know at the beginning
4:34
what you need to know before you go deep. In this session, it doesn't matter whatever you have
4:42
I promise you that you will learn something today, right? Okay, if you have a brand new app
4:51
be smart at the beginning. You need to be smart at the beginning
4:55
The first thing is making a good architecture choice. So you need to pick a good MVM framework
5:05
like Prism, React UI, MVV Lite, Fresh MVVN, and so on. But you can use what you like, doesn't matter
5:14
but pick a good MVVN framework. Another thing is, if you are going to use database
5:22
make sure that you have a good repository pattern. If you are new in the Summoning Forms world
5:32
of you have a small project, you can use Shell. Shell can be a good option because you will have probably all the tools
5:43
that you need to work with in an easy way. Also, you can pick dependencies wisely. Don't
5:54
go too heavy with dependencies. Try to make your app light and snappy. And you need to
6:05
add a unit test project or at least a mock configuration. This is because you need to
6:16
have a way to test your project, a way that tell you like, hey, it's happening something
6:25
right? And okay, I have this hashtag, Summoning Performance. In this session, I have, well
6:37
in this talk, I have separate sections in the presentation. In each session, I'm going
6:43
to share the resource that I use for accomplish these steps that I'm sharing with you. So if you
6:52
want to share your tips in that session, you can do that with the Summary Performance hashtag
7:01
right? And I'm going to read it at the end of that session. I have the application right here
7:11
on Twitter. Okay, if you already have an app, be smart for the long term. Do you have the
7:21
best architectures choice You need to do you need to you need to ask what that question but I write a book that it has seven
7:38
tactics for structure your project with summary forms if your priorities with
7:42
summary forms you have seven tactics that you can use today and it's free right and
7:50
Another thing, if you already have an app, I have some tips that I'm going to share with you. These tips are from the official documentation that Microsoft Summary has
8:10
So, we have in order to improve the performance of our SAMRIN form application, we need to enable the SAMRIN compiler
8:21
What is this? Oh, sorry. We need to enable the SAMRIN compiler, use compile binding and use fast render
8:34
render. These options are enabled by default but for older solutions you need to enable them manually
8:44
Reduce unnecessary binding, don't use binding for content that can be easily set statically
8:51
for example the name of the page, the title of the page, choose the correct layout, for example
8:59
if you have a stack layout and the stack layout is capable of having multiple children but you only
9:05
have a single child is wasteful you know what i mean um optimize layout performance use
9:15
synchronous programming and choose a dependence injection container carefully i have specific
9:20
tips that I recommend you to follow in the guide they provide because you will learn a lot if you
9:29
don't do it. Use a collection view instead of list view. Collection view is more flexible and
9:39
performant than the list view so it's better. If you are using a list view because something
9:47
happen. Be sure that you optimize the performance with initialization, scrolling, interactions
9:57
reduce the visual tree size, reducing the number of elements of a page will make your
10:05
page that render faster. And reduce the application resources, dictionary size. SAML, that is
10:17
specific to a page shouldn't be included in the application resource dictionary in the general in
10:23
the app summary right because the results will be parsed at the application startup instead when you
10:31
need to use it on that page right so don't overload the resource dictionary
10:39
Creating apps with summary info, examples and tips. I wrote an article talking about this topic. In this article I talk about the trip to the developer application that are your app, the purpose of your app and the developer
11:09
Also, I give you some general tips for increasing your application performance. In the performance section, I give you some resources that you can check. Also, an important tip, avoid using relative and absolute layout as much as you can, please
11:37
you will have all the resources in that post. In the same post, I give you some tips for Visual Studio and some tips that help you to choose the best MVVM framework for your needs
11:56
In the same way, you have three tips for your SAML and C-chart project
12:02
And I have a lot of tips there. So go check it
12:12
Here's the first resources for the performer, the base. I'm sharing with you the SAML and Mobile Resources Library that I have in my blog
12:23
You can see it here. the improve some uniform and performance here's the link that you can see all of those tips that
12:31
Microsoft give you and the sample activity from creating application in some uniforms and also I
12:39
don't have any people talking but remember you if you want to add something in that session you can
12:47
do it with the hashtag summary performance and also if you can use the
12:54
CCHAR live show hashtag too so let's talk about performance and optimization
13:06
I know that okay we have a start-up performance checklist I know that this
13:13
list is small but think about it and I promise you that you will see the
13:17
results immediately don't start all the savings that you have your application
13:24
right at the beginning use lazy load as possible don't dollar all your data on
13:31
the startup and think about user experience when you talk about we are
13:39
going to see all of those steps later but when you talk about data use your
13:47
data when you need it you don't need to download them together right okay here
14:01
we have a release configuration for Android so enable your layout compression enable startup tracing on Android here you have an example of linking set your linking
14:14
settings if your app before release you can do it with link a select assembly with link all and again
14:24
it's going to the panel of your needs you also will have a post talking about it but for release
14:32
configuration you have link or a set of a user assemblies and also if you have
14:43
some projects or building project you can able linker safe I'm going to write a
14:51
post talking about this because I think there is a few documentation for this We will see Follow me on social media and see what we can do
15:07
Use Android App Bundle. It has a better security and is better than the APK. APK. Sorry
15:18
For iOS, we have the LLBM optimization compiler. Enable that in iOS
15:29
So all the resources for the release configuration you can see here
15:33
Take care of the script shop. I have some posts here that are really great that I recommend you to see
15:44
is Summary in Android Builds, Summary in Android App Bundle, the Summary Show with Android
15:50
App Bundle, really nice session. That we know now also have a blog post to boot your Summary
15:58
from Startup Time, really nice post. Faster Startup Time with Startup Racing on Android
16:09
is also a good option to see. Okay. We also have resources for the linker. If you want
16:23
to know about linker, I have here Montemagno. Talk about investing time in the summary linker
16:32
for smaller app size so if you want to reduce the app size of your application that's a good
16:40
post that you need to read optimizing summoning apps and library with the linker
16:48
and linking on summoning android and linking on summoning ios and also you have a trick
16:56
that John Douglas shared with all of us. Performance. If we talk about performance
17:07
we need to talk about memory management. Let's talk about event handles. I know that these things
17:21
are something you hear again and again but trust me it's very important to
17:30
remember these rules just remember as a general step you need to subscribe from
17:42
events and remember to subscribe events then before the subscriber object is
17:51
dispose of. That's it. Keep that in mind at the beginning. Later you can read some
18:03
posts and maybe I will give you all the results that you need to apply best
18:09
practice but at the beginning just think about that and that's it. We are not
18:15
going to detail all those those things. Prevent immortal objects with reference
18:25
In .NET, any normal reference to another object is a strong reference, right? And
18:35
that's it. When you declare a variable of a type that is not primitive or value
18:43
type you are declaring strong reference for example here we have object A and
18:57
object B that have a strong reference to each other right and because of this
19:25
sorry, handle that for you. But in Xamarin iOS we need to be careful with that, right
19:37
But it doesn't matter, why? Because we can use weak reference to prevent immortal objects
19:46
a weak reference is created using an instance of the object to be trapped. So, object A
20:00
let me use this, okay, object A maintains a strong reference to object B, but object
20:12
be maintain a weight reference to object a and that's it with that you are all safe if you don't
20:26
do something like this your app is going to fall down okay load data efficiently when you're loading
20:40
a page you need to think about user experience when you need to be informative when you're
20:49
loading your view and it's data so you need to tell the user that you are doing something but
20:59
don't show a black uh a blank screen and doing your stuff in the background no tell the user like
21:10
hey i'm doing something a good practice is um lower the local content first i'm talking about
21:19
your views your component that you have locally and in the background then you can start lowering
21:27
your data so you can show for example an image you can show a tbc indicator a label whatever you want
21:38
drop something to the user load that data first and then in the background call your data and
21:46
populate what you need to populate your view you need to provide to user an experience of like
21:54
there is something happening and not just like there is nothing appearing on the screen
22:02
like I say you can use playholders label image etc something that I like in this case is defining states like my application is busy
22:20
complete this task I console this task and all of those you can use some animation with that you can play with that and make a good really user experience another thing is dumping things that can be said
22:41
statically I talked about before if you have to label for the temple and one of
22:50
of those describe something that for example a title and in the line below you have the title
23:02
of whatever you are talking why you need to bring a static text you can do it right in your summer
23:26
um resources and i seen nobody using the hashtag summary performance
23:37
so if you are there you want to share some tips please use the hashtag or follow me in social
23:47
media um resources that we have for performance memory management you have here i really recommend
23:58
you uh the post that michael software summoning for memory performance best practice really nice
24:07
also the the the summary show and the session they have uh memory management is really nice
24:36
Okay. I wrote a post talking about Async Awake and Task Best Practice. Async Awake is one of those topics that you feel you like, you know a lot, but then you don't
24:59
The reality is that there are a lot of different ways to use a single way and that's part of the problem
25:09
So in the article I give you some tips like how to invoke tasks, how to await multiple tasks and what you need to know about threats
25:23
and for tasks you have tips like how to return tasks maybe it's our basic but
25:35
read the post really and avoid using boy method in order to know the state of
25:47
the process that you have with your method and how to return tasks inside a
25:54
try cache or using block there are confusion in these kind of things so
26:01
read the post please I really recommend you to read the post believe me also I
26:13
I have tips for some of the forms and some plugin and extension for Visual Studio that
26:23
you can use. A really nice post I'm talking to you. There is some kind of steps that the effort that you need to do is really small
26:42
Please read the post. I also write an article talking about global error handling in summary forms
26:58
Many times we may encounter unhandled exceptions that are very difficult to detect a lot
27:10
must do so in order to handle error in your application you need a way to know
27:18
what happened with your application why your application doesn't work in the way
27:23
you want to work so there are global handles in each application that allow
27:29
you to receive notification of exceptions that you haven't handled elsewhere. That's a really nice post. I recommend you to read it. Not because I write it. No
27:46
Believe me. I do a lot of consultancies and this helps a lot. I will also write an article called
27:57
room safe tasks and common wrapper in summary form. When we work with tasks and
28:04
commands, most of the time we do common implementation of some scenarios where
28:13
we use repetitive code for example it's busy, try cache for a method that you
28:22
need to know what happened there is something happened allowing it set it up
28:26
the idea is to make a grapper that allow us to store all of this logic for us and
28:35
reuse that with test cover I have there some really nice tips that you can check
28:45
over there Here are the resources. I have here the blog post that I wrote, as in a way, and that best practice, global error handles with summary forms, room safe, task, and command wrapper in summary
29:08
I also I didn't talk about the plugin because it's an extra
29:16
But. I build a plugin that is called plugin validation rules that if your application have entries from users
29:28
Believe me, this plugin is going to be useful. In the long term
29:33
Really useful, even though it's not a long term application. See ya
29:42
Somebody put from a hashtag on Twitter, people, please let's go. Quality, let's talk about quality with resources
29:55
Okay, when we talk about icons, images, In access, the best thing you can do is using each platform
30:10
Yeah, that's it. Because the platform is going to adapt or use the files they need in the application they need
30:22
Remember, we have tablets, we have iPads, we have different kinds of devices and different kinds of resolutions. So the best thing is go to each platform
30:40
But if you have generic files like a small image that you will share with all of your application, you can use FIMBED resources
30:58
but avoid sharing larger files in a chair project please that's not help your
31:11
application also if you want a faster image loading a boy what I told you
31:21
putting a lighter files in that next standard project if you want to load
31:31
resources from the web don't do it for fundamental resources that your
31:41
application has for example loading icons and all of these things you can
31:47
have it in your application. Avoid that. And consider using GlideX for Android and Nuke for iOS
32:00
One line of code that will help you to load faster images. I'm not kidding with those libraries
32:11
And if you need to use images frequently, you can catch them with FFmatch login. That's a library too
32:22
Here are the resources for what I showed you. I have here a plugin for Visual Studio that is called MFAT2 Look for it It really nice
33:00
And also, I give you, if you are using shared images, if you want those images to be in each
33:10
platform you have a really nice plugin that is called Resize Dicer MT really nice you can see it
33:22
let's talk about quality but quality with dependencies I don't know if you like this kind of session because I'm just telling you the tips that you
33:38
need and sharing the resource that you need but we don't have time to detail all of those tips and
33:45
we need to cover a lot of things so sorry but wait for the pause or look for um the resources
33:54
that you need if you need something that i'm sharing with you right uh dependency what best
34:00
practice optimize dependency being used remote remove everything that is not being used for example it's try not get extra dependencies sometimes we replace
34:18
a library for another one but we forget we forgot that we need to remove the library that we use
34:36
before that's a common scenario so try to remove everything that you are not
34:45
using and also you can set your nugget package manager option to use backup
34:52
reference as a default do it for this
35:02
We have here just two blog posts. If you want to migrate a package that config file to package reference, you can do it. Here you have a blog post talking about it
35:21
And Jonathan Peppers also talk about under performance guide. And it's a really nice post that is talk about dependencies there
35:32
So go there and see what's Jonathan Peppers need to tell you
35:39
Okay let talk about let me see the time okay I running time let talk about quality and tools when we want to get all sort of useful data from our app is
36:04
is when we need to use Profiler. Profiler help us look for memory leaks
36:14
large images, large object, wide scope, cross-preference contents to prevent the garbage collector
36:27
from working properly. help us to measure a start of time operation time memory consumption CPU
36:43
profile so it helped you a lot and it's going to give you a lot of data that you
36:50
can use to help your application to be better, right? You have some profiler option that you
37:03
can use in case no one knows. I know that profilers is one of those things that
37:11
that most developers use, but it will increase your application performance a lot. Use them
37:23
You have the Summary Profiler, of course, you have its code instrument, and you have Android
37:33
Studio Profiler. You have those three options that you can use. In case you are wondering
37:40
how to start to use these tools, I give you all the resources you need later. Visual Studio
37:53
I have some extensions for you. For example, extensions that will help you a lot just in
38:05
telling them. For example, Sam Bright, help you to strengthen your summary forms development
38:15
and reduce your debugging cycle. You have SAML Styler. SAML Styler is Visual Studio
38:27
extension that format SAML based on a set of styling rules So if you are working in a team with this extension everybody will have the
38:48
the SAML style code in the same way so everybody can read what other people
38:56
write before, wrote before, right? So use that. I really like that extension and
39:05
also you have a sync method name feature. The easy way to yze and fit
39:14
method names for a single methods that the tension you use you need to use and
39:25
that's what it does it will have people that is maybe that is new in this
39:37
programming wall so you can use if you want or if you need
39:45
resources I have the resources here for quality tools I have the profiling
39:52
summoning iOS application with strument the profiling Android apps that are from from Microsoft you can see what they have there really nice
40:06
documentation I want to okay the best documentation in the world for a
40:16
framework that I have seen in my life is the Microsoft documentation I really
40:24
like because all the things that I need is over there of course there is something specific that
40:31
maybe you want to do and you are not going to see it there but the best things you can do when you
40:41
need something it's go to the official documentation that Microsoft has because it's really nice and
40:49
is gonna change your life if you are start using right do you understand right okay got it and also
41:00
you have here all the station for visual studio that i showed you before and we have
41:07
10 minutes for questions if you have questions this is the time
#Software
#Windows & .NET