In the event of a series of time-consuming operations such as network requests, read-write operations, and so on, in order to avoid blocking the main thread, we put these time-consuming operations into the sub-threads, and when the processing is complete, go back to the main thread to update the UI so that the main thread is not blocked. However, when creating the UI is usually done in the main thread, if you need to create more UI controls, there may be a very unfriendly lag, experience is poor, such as push to a certain viewcontroller, due to project requirements, The Viewcontroller created a more view and view sub-class, the page in the jump, there will be a very unfriendly lag phenomenon.
The simple way to do this is to use it directly.
ID) Afterdelay: (Nstimeinterval) Inmodes: (nonnull nsarray<nsstring *> *)];
This method has four parameters, the first parameter can be interpreted as the name of the method to invoke, and the second parameter indicates the parameters to be called by the method (if no argument, nil can be passed); The third parameter indicates how many seconds delay is performed (if you do not delay the execution of 0.0); The last argument is an array. The elements in the array are Runloop mode (Nsdefaultrunloopmode and Nsrunloopcommonmodes).
- (void) viewdidload {[Super viewdidload]; [Self performselector: @selector (SETUPUI) withobject:nil Afterdelay:0.0fInmodes:@[nsrunloopcommonmodes]];}//This code is executed in the current thread using the given specific runloop mode, and not necessarily in the main threads. If you want to ensure execution in the main thread, you can use the//PerformSelectorOnMainThread:withObject:waitUntilDone: or//performSelectorOnMainThread:withObject:waitUntilDone:modes: To replace- (void) Setupui {//the UI action that blocks the main thread can be placed here to execute for(intI=0; i<10000; i++) {UIView*view = [[UIView alloc] Initwithframe:cgrectmake (0, i*2+Ten,5,5)]; View.backgroundcolor=[Uicolor Redcolor]; [Self.view Addsubview:view]; NSLog (@"time consuming UI actions"); NSLog (@"%@", view); }}
Transferred from: http://www.jianshu.com/p/82c98bf6ef81
iOS development time-consuming handling of creating UI