nsoperation

Why does NSOperation disable automatic key-value observing?

*爱你&永不变心* 提交于 2019-11-27 10:49:38
问题 When working with a custom NSOperation subclass I noticed that the automatic key-value observing is disabled by the [NSOperation automaticallyNotifiesObserversForKey] class method (which returns NO at least for some key paths). Because of that the code inside of NSOperation subclasses is littered by manual calls to willChangeValueForKey: and didChange… , as visible in many code samples on the web. Why does NSOperation do that? With automatic KVO support people could simply declare properties

How do I use NSOperationQueue with NSURLSession?

余生长醉 提交于 2019-11-27 10:17:54
I'm trying to build a bulk image downloader, where images can be added to a queue on the fly to be downloaded, and I can find out the progress and when they're done downloading. Through my reading it seems like NSOperationQueue for the queue functionality and NSURLSession for the network functionality seems like my best bet, but I'm confused as to how to use the two in tandem. I know I add instances of NSOperation to the NSOperationQueue and they get queued. And it seems I create a download task with NSURLSessionDownloadTask , and multiple if I need multiple tasks, but I'm not sure how I put

Best practice to send a lot of data in background on iOS4 device?

一个人想着一个人 提交于 2019-11-27 09:53:12
问题 I have an app that needs to send data (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/or close the app (only iPhone4/iOS4 will be supported). Should I use threads/NSOperations or/and send data using existing asynchronous methods? Any ideas/best practices how to implement this? 回答1: OK, I'll answer my own question. First, like tc said, it's better to have this call on the

Generic NSOperation subclass loses NSOperation functionality

99封情书 提交于 2019-11-27 08:44:51
Today I've met one weird issue when I was trying to 'generalize' my 'CoreData importing operations'. It appeared that if I create a generic subclass of NSOperation the main() func won't be called. Simple example: class MyOperation<T: NSObject>: NSOperation { override func main() { println("My operation main was called") } } If you create an instance of this class and add it to the operationQueue you will see that it's main() isn't actually called. override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.operationQueue =

Do NSOperations and their completionBlocks run concurrently?

送分小仙女□ 提交于 2019-11-27 07:03:18
问题 I've got a bunch of NSOperations added to a NSOperationQueue . The operation queue has the maxConcurrentOperationCount set to 1, so that the NSOperations run one after the other. Now, in the completionBlock of a NSOperation I want to cancel all pending NSOperations by calling cancelAllOperations on the NSOperationQueue . Is it safe to do this? Can I be sure that the start -method of the next operation is called only after the completionBlock of the previous operation has been fully executed?

Trying to Understand Asynchronous Operation Subclass

主宰稳场 提交于 2019-11-27 06:42:11
I am trying to get started with using Operation s in a side project rather than having closure-based callbacks littered throughout my networking code to help eliminate nested calls. So I was doing some reading on the subject, and I came across this implementation: open class AsynchronousOperation: Operation { // MARK: - Properties private let stateQueue = DispatchQueue(label: "asynchronous.operation.state", attributes: .concurrent) private var rawState = OperationState.ready private dynamic var state: OperationState { get { return stateQueue.sync(execute: { rawState }) } set { willChangeValue

How to cancel NSBlockOperation

江枫思渺然 提交于 2019-11-27 06:29:35
I have a long running loop I want to run in the background with an NSOperation . I'd like to use a block: NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while(/* not canceled*/){ //do something... } }]; The question is, how to I check to see if it's canceled. The block doesn't take any arguments, and operation is nil at the time it's captured by the block. Is there no way to cancel block operations? Doh. Dear future googlers: of course operation is nil when copied by the block, but it doesn't have to be copied. It can be qualified with __block like so: //THIS MIGHT

NSBlockOperation or NSOperation with ALAsset Block to display photo-library images using ALAsset URL

血红的双手。 提交于 2019-11-27 06:29:28
问题 I am asking this question regarding my questions Display photolibrary images in an effectual way iPhone and Highly efficient UITableView "cellForRowIndexPath" method to bind the PhotoLibrary images . So I would like to request that answers are not duplicated to this one without reading the below details :) Let's come to the issue, I have researched detailed about my above mentioned issue, and I have found the document about operation queues from here. So I have created one sample application

Can you use cancel/isCancelled with GCD/dispatch_async?

匆匆过客 提交于 2019-11-27 04:01:25
I've been wondering, can you use cancel/cancelAllOperations/.isCancelled with a thread you have launched with GCD? Currently, I just use a boolean as a flag, to cancel the background process. Let's say you want to do a lot of processing in the background, while keeping the UI responsive so that you can catch a cancel button (or animate something to show the processor is working). Here's how we do it... @interface AstoundingView : UIView { BOOL pleaseAbandonYourEfforts; blah } @implementation AstoundingView // // these are the foreground routines... // begin, abandon and all-done // -(void

Which tasks are more suitable to NSOperation than GCD? [duplicate]

拟墨画扇 提交于 2019-11-27 00:15:50
问题 This question already has an answer here: NSOperation vs Grand Central Dispatch 8 answers Which tasks would be better suited to using NSOperation as opposed to using GCD when programming for the iPhone? To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other. 回答1: NSOperation is built on top of GCD, so the question is more about whether you use NSOperation or pass a block directly to GCD. An NSOperation is bulky and needs more boiler-plate codes