Slow application [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is it possible to call an action that contain heavy operations several times? like save button (I save the first object) and want to add others, but the app becomes so slow and I can't navigate to other views

You can always play around with heavy data saving related tasks in the background. You can distribute the content related stuff to various threads (queues), you need to separate the stuff which you are saving and if it is blocking your UI and making your app slow then you need to perform these heavy operations in the background.
Make sure, you do NOT perform any UI updation operation in the background. Try reading about GCD (how it works), how you can create a background Queue etc and how you can play around with it. I assume you might be using core data in order to save the contents on save button. Try reading about the Parent/Child Manage Context Objects. Play around with it a bit and move your heavy task to background, updation of UI will always be on Main or UI thread (which you can always do it by calling get_main_queue() if you are working with dispatch queues). Happy Coding. If you have specific code, which is doing this, then let us know. We will be glad to help. :)

You need to make sure that you aren't tying up the main UI thread of your application. A potential easy fix is to do your save operation on a different thread. Here is an example of how to do just that using Grand Central Dispatch: iPhone - Grand Central Dispatch main thread
Example:
//notice this saveQueue is a new dispatch queue that's been created.
dispatch_queue_t saveQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
dispatch_async(saveQueue, ^{
//this command represents your long running operation
doSaveOperation();
dispatch_async(dispatch_get_main_queue(), ^{
//always update your UI on the main thread!
[self showCompleteMessage];
});
});

Related

How to handle network requests that have to be finished in swift [duplicate]

This question already has answers here:
How to reloadData for tableview after data been prepared in a asynchronously method
(4 answers)
Closed 5 years ago.
So I'm currently stuck because the App I'm developing is handling network requests and I'm quite new to it.
The App needs to load some data first in order to even work properly. So I tried to execute the network request in the AppDelegate. However, as I understand it, network requests run asynchronously, which is why my table view does not load properly. So I tried reloading the tableview after the network request finished, which worked, but gave me the following message:
UITableView.reloadData() must be used from main thread only
My question is how to properly handle such a situation. What is the industry standard?
It tells you what to do, do it on main thread:
DispatchQueue.main.async {
// also put here the manipulation of the data model of the tableView
self.tableView.reloadData()
}
All the UI related manipulation should be done on the main thread. In this particular case the callback of the network call is executed on the background thread, so you need to dispatch the reloadData to the main thread.

iOS application have multiple main thread [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can we create multiple main thread in single application?
Why all UI changes made in main thread?
I have faced these question in interviews. I am unable to find any solution. Can any one help me to find the answers of this question? That will help me in future interviews and implementations in iOS app.
Thanks in advance!
There is only ever one main thread in your application, however tasks from multiple dispatch queues may execute on this thread. Tasks that are dispatched on the main queue are guaranteed to run on the main thread.
Generally with GCD you do not think about threads, but rather queues which are abstracted from the underlying threads. They provide a level of abstraction that makes it easier to manage tasks without being concerned about the number or state of the threads that are being used.
THe autolayout engine performs a number of calculations and applies rules in order to arrive at a layout solution. If the properties of a UI element change then the autolayout solution is invalidated and needs to be recalculated.
As the main queue is a serial dispatch queue, dispatching UI updates onto the main queue ensures that either the autolayout calculations are being performed or a UI property is being updated but not both at the same time (of course there are more than just these two activities happening on the main queue, but we are just addressing autolayout here)
No, there is only one main thread for the application that is created by system for application. Main thread is the thread that directly gives changes to user. Thread which is directly connect with user you can said.
Check the Apple Documentation, It states,
The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:
Calling dispatch_main
Calling UIApplicationMain (iOS) or NSApplicationMain (OS X)
Using a CFRunLoopRef on the main thread
As with the global concurrent queues, calls to dispatch_suspend,
dispatch_resume, dispatch_set_context, and the like have no effect
when used with queues returned by this function.
Now, let's take an example, If you are downloading an image and want to display it to user. Now if you will download it on main thread then it will block other UI till it completed download. So, you should not download it on main thread or synchronously . you should download it asynchronously and when download is completed, you should display it to user on main thread, so it will quick displayed to user after download is completed.

Will off loading tasks to background threads improve overall performance in iOS?

While using XCode Time Profile, I see that most of my function calls are being performed in the main thread. Since they're not UI related, I want to move them to the background thread. Will offloading these tasks to the background thread improve the performance of my app or would it still be the same. I know that at least it'd benefit updating the UI. How to exactly measure the performance of an app using Instruments, which profiling module should I use?
Thanks
Offloading tasks that don't require a main thread is the way to go. Your main thread only deals with the action that demand user attention or are related to UI actions.
I don't know what do you mean by performance when you talk about delegating background threads. Indeed, you are putting forward a better UI/UX but performance as such can't only be uplifted through threads.
I recommend you profile your application and see what objects are you creating?
-Monitor any leaks.
-Consider doing a time profiling.
-Check if your I/O and CPU activities are optimum.
These will surely help you nail down and performance related issues.
(NB: Your 5% of time should also go to profiling your app.)
If you took , off loading task in background thread, the main benefit will be that it will remove code dependency that means your UI related task will not be dependent on non-UI related task that will increase you'r app's performance.
And This whole process is called concurrency programming in iOS which you can refer from here.
For instance, if I am downloading a file from the internet and I want to update the user on the progress of the download, I will run the download in the priority default queue and update the UI in the main queue asynchronously.
// Doing something on the main thread
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
// Perform long running process
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
});
});
// Continue doing other stuff on the
// main thread while process is running.
For profiling you can refer this link https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeProfile.html

Using main and background threads [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to have some clarify about it.
I know that I need to update UI in main thread. Are there anything else?
What I need to do in main thread and what in background threads?
In addition to UI updates, as a broader thread-safety strategy, people will often dispatch their model updates to the main thread as a simple synchronization technique, too.
Synchronization is ultimately the process of assuring that an object is in a logically consistent state, i.e. that while an object is being used on one thread, that it isn't simultaneously being mutated by some other thread. Traditionally, one might accomplish this by employing locks (e.g. NSLock, #synchronized, etc.) but you can also achieve this by dispatching all interaction with a particular object to a serial queue. While you can replace locks with a dedicated serial queue, in many cases it's just as easy to dispatch all updates to the object in the main queue. It turns out to be a convenient way to synchronize your model objects that might otherwise would have been used/mutated by separate threads.
For more information, see the Eliminating Lock-Based Code section in the Migrating Away from Threads chapter of the Concurrency Programming Guide.
It all depends.
Modern OS take advantage of the multiple cores or virtual CPUs, so when you run your app, the OS defines what to run where, and usually your program runs in multiple threads.
If there are data dependencies, then you should run things in specific threads, unless you run stuff in background or different threads, then you can implement notifications to ensure that the data you need is ready when you need it. You should also take into account the thread safe nature of the the different ways to define properties. So... other than the UI stuff in main, you can run anything pretty much wherever you want.

Dispatching to main queue when I'm already in main thread [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Tell me if this make sense. This is an iOS question.
I'm seeing code where it is already in the main thread, but the code would dispatch_async all sorts of UI code to the main thread's queue. Layouts, animations etc etc.
I was told this some how speeds up responsiveness (example, when pushing a view controller, you would dispatch other UI ops there so it won't block the push transition.
This doesn't make sense because first it is dangerous, second, it doesn't guarantee when the UI code gets run(even though it will probably run with milliseconds). The only good reason I can see is for it to guarantee that the UI code is not accidentally run in a different thread.
What do you guys think?
There are definitely times you employ this pattern of dispatching back to the main queue, so I might not be too quick to dismiss it or label it as "dangerous" (though the way you characterize it, it does sound suspicious). You should share some code samples of how you're seeing this pattern being used, and we can comment further.
When would you dispatch to the main queue? The prototypical example is when you're doing something on a background queue, but then want to dispatch the UI updates back to the main queue, e.g.:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do something time consuming here, like network request or the like
// when done, update the UI on the main queue:
dispatch_async(dispatch_get_main_queue(), ^{
// update the UI here
});
});
But I'm assuming this is not the sort of dispatch to the main queue you're talking about. I'm assuming from your comment where you have something on the main queue dispatching asynchronously right back to the main queue itself.
The reason you would do that is if you don't want the dispatched code to run immediately, but rather to be queued for the next iteration of the run loop. This lets the autorelease pool drain, lets the current method complete (and release any resources it might have been using), lets other dispatched tasks run first, gives the UI a chance to reflect any changes you may have initiated, etc.
A few examples of when some developers might do this include:
You might use this pattern if you want a method call itself recursively, but you want to yield back to the run loop, to let resources be freed, let the UI reflect any changes, etc. You're basically saying "ok, let this method finish, but in the next run loop, run this method again."
A little more suspect, I've seen this pattern in viewDidLoad where you want to give auto layout a chance to "catch up" and update the frames. E.g. there is a common third-party progress indicator that won't work if you just invoke it from viewDidLoad, but it works if you dispatch that update back to the main queue.
Having articulated reasons why I've seen developers dispatch back to the main queue from the main queue, itself, I must confess that many of these patterns suffer from code smell and are often better accomplished via different patterns. But these are a few examples that I've seen.
But, again, if you want assistance on your particular code sample, you have to share it with us. We can't tell what the developer intended without seeing the code sample.

Resources