In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how iOS uses multithreading to improve data concurrent access, which is very detailed and has a certain reference value. Friends who are interested must finish it!
The first example
This example demonstrates some of the problems with IO performance. Later on, I will use multithreading technology to speed up the execution of the code. My example is simple:
In the first test, I will load and display a sequence of pictures in a table view. Then, I'll show you the problem with scrolling performance: you can't scroll the table until all the pictures in the current state are returned.
In the second test, I will use multithreading to accelerate the execution of the program. You will see that scrolling performance will get better when you are waiting for the picture to load.
Note: in both cases, my example does not cache images so that you can see the difference more clearly.
The test results shown in Table 6-1 are based on the Core Animation test results, and you can see the application test results in a real environment.
Table 6-1 shows the fps at load time when processing and running applications on iPhone OS. You can see that multithreading can significantly speed up the loading process. In the absence of multithreading, the loading process will block UI and your application will be suspended.
I will show you the source code, and I will make some simple explanations before delving into the concept. Listing 6-1 is the source code for the first test.
Listing 6-1. First Benchmark; This Code Runs Inside the UITableViewDataSource's Method.
-(UITableViewCell *) tableView: (UITableView *) tableView
CellForRowAtIndexPath: (NSIndexPath *) indexPath {
Static NSString * CellIdentifier = @ "Cell"
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
If (cell = = nil) {
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier]
}
NSURL * p_w_picpathURL = [NSURL URLWithString: [self.p _ w_picpathsArray objectAtIndex:indexPath.row]]
Cell.p_w_picpathView.p_w_picpath = [UIImage p_w_picpathWithData: [NSData dataWithContentsOfURL:p_w_picpathURL]]
/ / Configure the cell.
Return cell
}
Listing 6-2 simply shows the usual practice of returning pictures in asynchronous code. For simplicity, asynchronous code is actually not shown or discussed here.
Listing 6-2. Second Benchmark-Getting the Image Through a Background Thread
/ / Customize the appearance of table view cells.
-(UITableViewCell *) tableView: (UITableView *) tableViewcellForRowAtIndexPath: (NSIndexPath *) indexPath {
Static NSString * CellIdentifier = @ "Cell"
ImageCell * cell = (ImageCell *) [UIUtilities getCellWithTableView:tableViewcellIdentifier:CellIdentifiernibName:@ "ImageCell"]
/ / Configure the cell.
NSURL * p_w_picpathURL = [NSURL URLWithString: [self.p _ w_picpathsArrayobjectAtIndex:indexPath.row]]
[cell.contentImage displayImageWithURL:p_w_picpathURL]
Return cell
}
# import # import "ImageFetcher.h"
@ interface UIImageView (Network)
-(void) displayImageWithURL: (NSURL *) url
@ end
# import "UIImageView+Network.h" # import "ImageFetcher.h"
@ implementation UIImageView (Network)
-(void) p_w_picpathFetcherFinished: (ImageFetcher *) fetcher {
Self.p_w_picpath = fetcher.p_w_picpath
}
-(void) displayImageWithURL: (NSURL *) url {
Self.p_w_picpath = nil
If (url) {
/ / This code will run in the background thread and callback when it retrieves// p_w_picpath
[ImageFetcher loadImageWithURL:url delegate:self]
}
}
@ end
In the first test, the cell of table view is returned inside the method, where I get the image. For this line of code, iOS will stop and wait for the picture to return from the network. After that, it continues to return cell and display cell to UI. This waiting process stops the application, which is why you can't scroll table view quickly in the first test.
For the second test, I used asynchronous code, which is another form of multithreading, but the underlying library will handle multithreaded code for you. With this code, the main process is freed from the process waiting for the download to complete. So, in the second test, you can scroll table view without any problems.
Benefits of multithreading
In iPhone applications, you should consider using multithreading in some cases.
Take advantage of all cores and processors: (a processor always has multiple cores, and the kernel is actually a computing unit. Currently, iPhone 4 has only one processor and core, but iPhone 5 may have more cores, so you will be able to take advantage of all available processors to improve the performance of your application.
Modeling: you may want to try to model from real-world behavior. For example, consider a solution where you have 12 different types of tasks to do (solving bug, interviewing system administrators, creating your next product presentation slide, etc.), and only one complex task (solving 12 bug). The latter solution is simple, and you only have one work queue to complete. The first is a more complex solution where you can assign a thread to each task.
Processing IO tasks: typically, IO (network and file) tasks take time to return data to the application. Therefore, if you use a thread to process, your application will stop working and spend time waiting for data. Using multithreading can help you split the IO thread and merge it into the main thread after it receives all the data.
More sensitive UI response: all d GUI applications, such as iPhone, start with only one thread, meaning that all application code is executed through main event loop (also known as main run loop). Event loop is the logic that runs the input event response when the application receives an input event from the user (for example, click, slide, double click). The longer the application is executed in event loop, the less sensitive the UI response will be.
Do some logic processing in the background: this is also a very important part of the iPhone application code. In some cases you may need to deal with some big data, such as running an XML parsing algorithm to parse the data. This is also related to the response of UI: the less work is done on the UI thread, the better the user experience the program will have.
These are all the contents of the article "how iOS uses multithreading to improve data concurrent access". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.