Multithreading knowledge Points (iii)

Source: Internet
Author: User

    1. Nsoperationqueue as a global queue, you can set the maximum number of concurrent

-(Nsoperationqueue *) queue

{

if (_queue = = nil) {

_queue = [[Nsoperationqueue alloc]init];

Maximum number of concurrent

_queue.maxconcurrentoperationcount =}

return _queue;

}

2 Set the action to pause, continue, cancel

-(Ibaction) Cancle: (ID) Sender {

Cancel operation

[Self.queue cancelalloperations];

NSLog (@ "Cancel");

}

Pause the action that has not been performed, the operation that is being performed continues execution

-(Ibaction) suspend: (ID) Sender {

Suspended Pause

[Self.queue Setsuspended:yes];

NSLog (@ "pause");

}

Go on

-(Ibaction) Jixu: (ID) Sender {

[Self.queue Setsuspended:no];

NSLog (@ "continue");

}

3. The time-consuming operation of the download is performed in the child thread, and the dead loop is executed inside the child thread.

4. Use [Self.queue Setsuspended:yes] to suspend the operation inside the queue, the operation can not be too large, or the task will not stop

5. Refresh the UI if you add too much, the Uishua ' x refresh will speed up, if only the same refresh operation, only need to determine the number of operands, such as

if (Self.queue.operationCount = = 0) {

[Self.queue addoperationwithblock:^{

Refresh ui//in the main thread execution

}];

If there is more than one action to refresh the UI, you can add an action cache pool

6.//Set the quality of service iOS8 after IOS8 is recommended before the priority level

[Op setqualityofservice:nsqualityofserviceuserinteractive];

7. Add multiple actions (action array)

[Self.queue ADDOPERATIONS:@[OP,OP2] waituntilfinished:no];

8. Listening operation completed

[Op2 setcompletionblock:^{

NSLog (@ "OP2 completionblock");

}];

9. Set operation dependencies, can be different operations inside the queue

[Op2 Adddependency:op];

[Op3 ADDDEPENDENCY:OP2];

10. Download the pictures according to the website

Get Picture link Address

Nsurl *url = [Nsurl URLWithString:model.icon];

For picture download, binary

NSData *data = [NSData Datawithcontentsofurl:url];

Convert the binary into UIImage

UIImage *img = [UIImage imagewithdata:data];

11. Get the model to assign a value to the cell the cell control is lazy-loaded

12. Empty memory when a memory warning is received

Called when a memory warning is received

-(void) didreceivememorywarning

{

Empty the picture cache pool

NSLog (@ "didreceivememorywarning");

}

13. Set up a picture cache pool (dictionary)

-(Nsmutabledictionary *) Imgcache

{

_imgcache = [Nsmutabledictionary dictionarywithcapacity:10];

}

return _imgcache;

}

To add a key-value pair to a dictionary

[Self.imgcache setobject:img ForKey:model.icon];

SELF.IMGCACHE[MODEL.ICON] = img;

14. In order to prevent the time-consuming operation, TableView cell's imageview above picture appears the download dislocation situation, may when the download completes, refreshes TableView

15. In order to save the user's traffic, you can save the downloaded picture to the picture buffer Pool (dictionary), first determine the cache pool inside the release of the image, no more to download

Download is a time-consuming operation, it may have been added to the queue when it is not downloaded, but the picture is considered empty, so the operation cache Pool (dictionary) is set, and the operations that are near the existing one do not continue to be added to the queue

16. Circular reference Self, Queue,operationcache, op->block->self

17. Create a taxonomy for NSString nsstring+sandbox.h

Create three class methods: Get the Shahe path

Get the cache path

-(Instancetype) Appdendcache

{

return [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject] Stringbyappendingpathcomponent:[self Lastpathcomponent]];

}

Get Documents Path

-(Instancetype) appdenddocuments

{

return [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) Lastobject] Stringbyappendingpathcomponent:[self Lastpathcomponent]];

}

Get temp Path

-(Instancetype) appdendtemp

{

return [Nstemporarydirectory () stringbyappendingpathcomponent:[self lastpathcomponent];

}

Multithreading knowledge Points (iii)

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.