Time-consuming operations for creating a UI in iOS development and time-consuming ios development ui

Source: Internet
Author: User

Time-consuming operations for creating a UI in iOS development and time-consuming ios development ui

When there are a series of time-consuming operations such as network requests and read/write operations in the project, in order to avoid blocking the main thread, we will put these time-consuming operations in the Child thread for processing. After the processing is complete, return to the main thread to update the UI, so that the main thread will not be blocked. However, when creating a UI, it is generally executed in the main thread. If there are many UI controls to be created, unfriendly freezing may occur, resulting in poor experience, for example, when pushed to a ViewController, a large number of view and view sub-classes are created in the ViewController due to project requirements. During page Jump, the page may experience unfriendly freezing.
In this case, the simple method is to use it directly.

[self performSelector:(nonnull SEL) withObject:(nullable id) afterDelay:(NSTimeInterval) inModes:(nonnull NSArray<NSString *> *)];

This method has four parameters. The first parameter can be understood as the name of the method to be called. The second parameter indicates the parameter carried by the method to be called (if there is no parameter, pass nil ); the third parameter indicates the number of seconds of execution delay (if you do not need to delay the execution, you can pass 0.0). The last parameter is an array, and the elements in the array are RunLoop mode (NSDefaultRunLoopMode and nsunloopcommonmodes ).

-(Void) viewDidLoad {[super viewDidLoad]; [self defined mselector: @ selector (setupUI) withObject: nil afterDelay: 0.0f inModes: @ [nsunloopcommonmodes];} // This code is executed using the given Runloop mode in the current thread, not necessarily in the main thread. If you want to ensure that the task is executed in the main thread, you can use // your mselecw.mainthread: withObject: waitUntilDone: Or // your mselecw.mainthread: withObject: waitUntilDone: modes: instead of-(void) setupUI {// The UI operation that blocks the main thread can be put here for (int I = 0; I <10000; I ++) {UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, I * 2 + 10, 5, 5)]; view. backgroundColor = [UIColor redColor]; [self. view addSubview: view]; NSLog (@ "time-consuming UI operation"); NSLog (@ "% @", view );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.