Multithreading for iOS development nsobject

Source: Internet
Author: User

1 , NSObject the Multithreading Method ( When you use it, use it. @autoreleasepool {} Wrap it up. )

To open a background execution task:

-(void) Performselectorinbackground: (SEL) Aselector withobject: (ID) arg

Apple's bottom layer allows the UI to be updated in a background thread using the Performselectorinbackground method, which is strongly not recommended!

Notifies the main thread of a method to perform a task in the background threads:

-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait

Wait parameter:

YES: The thread is blocked until the call method finishes

No: The thread will not be blocked and the other tasks on the main thread will continue to execute

Get thread Information:

[Nsthread CurrentThread]

Thread hibernation:

[Nsthread sleepfortimeinterval:2.0f];

Set the thread to sleep for 2 seconds

Characteristics:

Simple to use, light-weight

Cannot control the number of threads and the order of execution

2 , NSObject Considerations for Multithreading methods

NSObject's multithreaded approach uses Nsthread's multithreaded technology.

And Nsthread's multithreading technology does not automatically use @autoreleasepool.

when using NSObject or Nsthread Multithreading Technology, if you are involved in object assignment, you need to manually add @autoreleasepool .

3 , @autoreleasepool

IOS memory Management in development:

(1) In iOS development, there is no garbage collection mechanism in Java or C #

(2) Using ARC development, only at compile time, the compiler automatically adds retain, release, and Autorelease based on the code structure

How the auto-release pool works:

(1) objects marked as Autorelease are added to the most recently created auto-release pool after the scope has been scoped

(2) When the auto-free pool is destroyed or exhausted, release messages are sent to all objects in the auto-free pool

(3) Each thread needs to have @autoreleasepool, otherwise a memory leak may occur, but using nsthread multithreading technology does not create an auto-free pool for the background thread

4 , auto-release pool common interview code

for (int i = 0; i < ++i) {

NSString *str = @ "Hello World";

str = [str stringbyappendingformat:@ "-%d", I];

str = [STR uppercasestring];

NSLog (@ "%@", str);

}

Q: What are the problems with the above code? If the number of loops is very large, how should I modify it?

A: The loop creates a NSString object with too many cycles to cause a memory leak. If the number of cycles is relatively large, you can use @autoreleasepool{} to wrap the entire loop, if each cycle will cause a memory leak, the loop content is wrapped up with @autoreleasepool{}.

Multithreading for iOS development nsobject

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.