Tham khảo tài liệu 'phát triển ứng dụng cho iphone và ipad - part 29', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Safely Threading with Core Data 249 In order to implement a subclass of NSOperation at a minimum you need to implement a custom initializer method that you use to configure your object for use and a main method that will perform the task. You can also implement any other custom methods that you need such as accessor methods to get at the operation s data or dealloc to free memory allocated by the operation. Once you have created your operation classes you need to tell the system to execute them. You accomplish this by creating an instance of NSOperationQueue and adding your NSOperation subclasses to the queue. The system uses operation queues to schedule and execute your operations. The system manages the queues which handle the details of scheduling and executing your threads for you. It is possible to configure dependencies between operations when adding operations to an operation queue. You can also prioritize operations when you add them to a queue. Core Data Threading Example As I mentioned this example application will generate five random numbers and pause for 1 second between each number to simulate a time consuming synchronous operation. This code will block the main thread causing the user interface to become unresponsive while you are generating the random numbers. Then you will take the random number generation and move it on to its own thread. Once you do this you will see that the user interface remains responsive while you generate the random numbers. 3 In this example you will be inserting data into the Core Data context on an off thread. This is generally not recommended however I am doing it in this example to demonstrate the mechanics of threading with Core Data. In production applications you should generally only use threads to perform time consuming reads and queries from Core Data and not to perform inserts or updates. To get started open Xcode and start a new Navigation-based application. Make sure that Use Core Data for storage is checked.