Ios development-NSOperation introduction, ios development-nsoperation

Source: Internet
Author: User

Ios development-NSOperation introduction, ios development-nsoperation

Introduction:

1. NSOperation is Apple's object-oriented encapsulation of GCD, Which is OC

2. NSOperation also provides some functions that are not particularly easy to implement by using GCD.

3. Add the operation to the queue and the operation will be "Asynchronous" immediately

4. NSOperation is an abstract class and does not have the ability to encapsulate operations. Its subclass must be used.

1> NSInvocationOperation

2> NSBlockOperation

3> Custom classes inherit NSOperation and implement internal methods

Code implementation:

Example 1: NSInvocationOperation

@ Interface TBViewController ()

@ Property (nonatomic, strong) NSOperation * myQueue;

@ End

// Lazy loading

-(NSOperationQueue *) myQueue

{

If (_ myQueue = nil ){

_ MyQueue = [[NSOperationQueue alloc] init];

}

Return _ myQueue;

}

 

 

-(Void) touchesBegan :( NSSer *) touches withEvent :( UIEvent *) event

{

[Self opDemo1];

}

-(Void) opDemo1

{

// Operation

NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (downLoadImage) object: nil];

// Start the operation. If the start method is used, the operation will be executed in the current thread.

// [Op start];

 

// Add the operation to the queue and the operation will be "Asynchronous" immediately

[Self. myQueue addOperation: op];

}

// Download the image

-(Void) downLoadImage

{

NSLog (@ "Download image: % @", [NSThread currentThread]);

}

 

Example 2: NSBlockOperation

// NSOperationQueue instantiates a concurrent queue.

-(Void) touchesBegan :( NSSer *) touches withEvent :( UIEvent *) event

{

[Self opDemo1];

}

-(Void) opDemo2

{

// Operation

For (int I = 0; I <10; I ++)

{

NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock: ^ {

NSLog (@ "block ==%@", [NSThread currentThread]);

}];

// Added to the queue. After running, you can see that the queue is a "concurrent" queue.

[Self. myQueue addOperation: op];

}

}

 

Example 3: Add a block directly

-(Void) touchesBegan :( NSSer *) touches withEvent :( UIEvent *) event

{

[Self opDemo3];

}

-(Void) opDemo3

{

For (int I = 0; I <10; I ++)

{

[Self. myQueue addOperationWithBlock: ^ {

NSLog (@ "=====%@", [NSThread currentThread]);

}];

}

}

Example 4: Inter-thread Communication

-(Void) touchesBegan :( NSSer *) touches withEvent :( UIEvent *) event

{

[Self opDemo4];

}

-(Void) opDemo4

{

[Self. myQueue addOperationWithBlock: ^ {

NSLog (@ "Download image: % @", [NSThread currentThread]);

// The UI and mainQueue main queue columns need to be updated after the download is complete.

[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {

NSLog (@ "main % @", [NSThread currentThread]);

}];

}];

}


10 ios sources and introduction

The interview is an important part for both recruitment and application. Especially for developers, the technical problem in the interview is not only an enterprise's test of the candidate's skills and accumulation, it is also a good opportunity for developers to test themselves. For iOS and Mac development, because the skills are relatively new, enterprises also have a large demand for developers in this area, so the interview requirements may not be very high, generally, some basic knowledge about Cocoa and OC is acceptable. However, for iOS or Mac developers of an enterprise that wants to have a technical foundation, two to three top skilled technical personnel lead some junior developers, project completion should be a common component.

Cocoa, especially the development of CocoaTouch, is very easy to get started with, but there are many hidden details and principles behind it. On the one hand, the basics are not skillful and clear (for example, from
AppDelegate starts to use code to build ViewController, or clearly describe stack and heap concepts ), on the other hand, we do not know much about more advanced development (such as multithreading, asynchronous network processing, or various frameworks starting with Core ). These contents are very important, but iOS developers may be more or less weak on these issues. Here I have compiled a list of questions for higher-level iOS developers, lists the technologies that should be mastered and understood by ten senior Cocoa/CocoaTouch development engineers at Leader level. This list does not provide standard answers, because these questions do not themselves have standard answers. There are different answers to different understandings of these contents. But basically, if you are dealing with a senior Cocoa developer, at least the expected answer is "Contact" and you can talk about it in combination with your own experience, to the point where they can understand each other's intentions and methods. It is better to have good opinions and specific explanations in two or three of the fields. This kind of knowledge coverage and depth investigation can reflect the developer's technical level. If most of the content in the list has never been touched or heard of it, it may be far from such a stage of senior Cocoa development.

Then, the interview starts.

You have used Objective-C Runtime programming (Runtime
Programming? If you have used it, what have you done with it? Do you still remember the names of the related header files or some methods you are using?

Multi-threaded Core
Data? Which of the NSPersistentStoreCoordinator, NSManagedObjectContext, and NSManagedObject need to be created or transmitted in the thread? What policies do you implement?

The content of the series starting with Core. Whether CoreAnimation and CoreGraphics have been used. What is the connection between the UI framework and the CA and CG frameworks? What animation or image content has been done with CA and CG respectively. (If necessary, some Quartz content can also be involved)

Have you used CoreText or CoreImage? If you have used CoreText or CoreImage before, let's talk about your experience using CoreText or CoreImage.

What is the difference and usage between NSNotification and KVO? When should notifications be used and KVO be used? Is there any difference in their implementation? If you use
CAN protocol and delegate (or the Array of delegate) implement similar functions? If possible, what are the potential problems? If not, why? (Although protocol and delegate are already broken in the interview ...)

Have you used NSOperationQueue? If you have used or understood it, why are you using NSOperationQueue? Describe the differences and similarities between the two and GCD (note: the implementation mechanism and applicability of the two can be described ).

Since we have mentioned... the remaining full text>
 
IOS multi-thread programming, can there be multiple NSOperationQueue?

Yes! Each operationQueue is independent of each other.

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.