I am developing a custom camera application.What I am doing is , I am taking picture using camera and showing them in same VC bottom of the screen.
I am storing the image in local dictionaries and NSDocument directory path. If the picture is in local dictionaries it will take from local dictionary else it will take from the NSDocument directory path.
After memory warning received, I just nil the dictionary ,so it will take the images from the NSDocument directory path.
Using both will showing images in slow process.My UI got not that much good in showing images.
So I want to store the images in NSDocument directory path using NSOperation.
I don't have much knowledge about NSOperation. I have searched in google , I am just getting swift tutorials while I require help in Objective C.
So please can anyone explain the NSOperation
and NSOperationQueue
with examples?
The NSOperationQueue also adds a number of benefits to the mix. For example, you can specify the maximum number of queued operations that can run simultaneously. This makes it easy to control how many operations run at the same time or to create a serial operation queue.
An abstract class that represents the code and data associated with a single task.
An operation queue invokes its queued Operation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it.
You can safely use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object. Operation queues use the Dispatch framework to initiate the execution of their operations.
Apply this for each work :
// Allocated here for succinctness.
NSOperationQueue *q = [[NSOperationQueue alloc] init];
/* Data to process */
NSData *data = [@"Hello, I'm a Block!" dataUsingEncoding: NSUTF8StringEncoding];
/* Push an expensive computation to the operation queue, and then
* display the response to the user on the main thread. */
[q addOperationWithBlock: ^{
/* Perform expensive processing with data on our background thread */
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
/* Inform the user of the result on the main thread, where it's safe to play with the UI. */
/* We don't need to hold a string reference anymore */
}];
And you can also apply without NSOperationQueue :
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Your Background work
dispatch_async(dispatch_get_main_queue(), ^{
// Update your UI
});
});
Try these more :
NSOperationQueue addOperationWithBlock return to mainQueue order of operations
http://landonf.org/code/iphone/Using_Blocks_1.20090704.html
https://videos.raywenderlich.com/courses/introducing-concurrency/lessons/7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With