NSOperation multithreading in iPhone development applications

Source: Internet
Author: User

IPhone DevelopmentApplication in progressNSOperation MultithreadingUse is the content to be introduced in this article. First, create a Thread class, RequestOperation, which inheritsNSOperationThen, in the Controller class, create an NSOperationQueue object and add the line to the sequence. It will automatically get the thread we added from NSOperationQueue, and then run the starting method.

 
 
  1. # Import "RootViewController. h"
  2. @ Implementation RootViewController
  3. # Pragma mark-
  4. # Pragma mark View lifecycle
  5. -(Void) buttonClicked :( id) sender {
  6. _ Queue = [[NSOperationQueue alloc] init];
  7. // The first request
  8. NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http: www.google.com"];
  9. RequestOperation * operation = [[RequestOperation alloc] initWithRequest: request];
  10. [_ Queue addOperation: operation];
  11. [Operation release];
  12. // Second request
  13. // NSURLRequest * request2 = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http: www.baidu.com"];
  14. // RequestOperation * operation1 = [[RequestOperation alloc] initWithRequest: request2];
  15. // Operation1.message = @ "operation1 ---";
  16. // [_ Queue addOperation: operation1];
  17. }
  18. # Import <Foundation/Foundation. h>
  19. @ Interface RequestOperation: NSOperation {
  20. NSURLRequest * _ request;
  21. NSMutableData * _ data;
  22. NSString * message;
  23. }
  24. @ Property (nonatomic, retain) NSString * message;
  25. -(Id) initWithRequest :( NSURLRequest *) request;
  26. @ End
  27. //
  28. // RequestOperation. m
  29. // NSOperation
  30. //
  31. // Created by wangqiulei on 8/23/10.
  32. // Copy 2010 _ MyCompanyName _. All rights reserved.
  33. //
  34. # Import "RequestOperation. h"
  35. @ Implementation RequestOperation
  36. @ Synthesize message;
  37. -(Id) initWithRequest :( NSURLRequest *) request {
  38. If (self = [self init]) {
  39. _ Request = [request retain];
  40. _ Data = [[NSMutableData data] retain];
  41. }
  42. Return self;
  43. }
  44. -(Void) dealloc {
  45. [_ Request release];
  46. [_ Data release];
  47. [Super dealloc];
  48. }
  49. // If YES is returned, it indicates asychronously processing.
  50. -(BOOL) isConcurrent {
  51. Return YES;
  52. }
  53. // Start processing
  54. -(Void) start {
  55. If (! [Self isCancelled]) {
  56. NSLog (@ "% @", self. message );
  57. NSLog (@ "------------- % d", [self retainCount]);
  58. [NSURLConnection connectionWithRequest: _ request delegate: self];
  59. }
  60. }
  61. // Obtain data
  62. -(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {
  63. // Add data
  64. [_ Data appendData: data];
  65. NSLog (@ "% @", _ data );
  66. }
  67. // Http request ended
  68. -(Void) connectionDidFinishLoading :( NSURLConnection *) connection {
  69. }
  70. @ End

Summary:IPhone DevelopmentApplication in progressNSOperation MultithreadingI hope this article will be helpful to you!

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.