nsoperation

Default value of maxConcurrentOperationCount for NSOperationQueue

橙三吉。 提交于 2019-12-17 16:32:13
问题 As the title suggests, what is the default value of the maxConcurrentOperationCount for NSOperationQueue? Is it set to a value of 1? 回答1: From the documentation, The maximum number of concurrent operations set explicitly on the receiver using the setMaxConcurrentOperationCount: method. If no value has been explicitly set, this method returns NSOperationQueueDefaultMaxConcurrentOperationCount by default . So it is NSOperationQueueDefaultMaxConcurrentOperationCount . If this is set, it will

Generic NSOperation subclass loses NSOperation functionality

时光毁灭记忆、已成空白 提交于 2019-12-17 09:47:17
问题 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

How to cancel NSBlockOperation

不问归期 提交于 2019-12-17 08:18:08
问题 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? 回答1: Doh. Dear future googlers: of course operation is nil when

How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift

落爺英雄遲暮 提交于 2019-12-17 04:53:19
问题 I have an app that has to download multiple large files. I want it to download each file one by one sequentially instead of concurrently. When it runs concurrently the app gets overloaded and crashes. So. Im trying to wrap a downloadTaskWithURL inside a NSBlockOperation and then setting the maxConcurrentOperationCount = 1 on the queue. I wrote this code below but it didnt work since both files get downloaded concurrently. import UIKit class ViewController: UIViewController,

How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift

不羁岁月 提交于 2019-12-17 04:53:03
问题 I have an app that has to download multiple large files. I want it to download each file one by one sequentially instead of concurrently. When it runs concurrently the app gets overloaded and crashes. So. Im trying to wrap a downloadTaskWithURL inside a NSBlockOperation and then setting the maxConcurrentOperationCount = 1 on the queue. I wrote this code below but it didnt work since both files get downloaded concurrently. import UIKit class ViewController: UIViewController,

Looping dispatch_after in a method causes many simultaneous dispatches when method is rerun

倖福魔咒の 提交于 2019-12-14 01:07:04
问题 I'm creating a simple game. I have the following code: - (void)doStuff { double delayInSeconds = [NSNumber randomFloatBetweenLowerBound:0.8f upperBound:2.6f]; // Own category on NSNumber returns random float. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // // Do stuff // if ([self shouldDoMoreStuff]) { [self doStuff]; } }); } This method is run in viewDidLoad , but it is also

Swift NSBlockOperation() Leak: cannot make NSBlockOperation() weak

大城市里の小女人 提交于 2019-12-13 19:47:43
问题 To avoid a memory leak when using NSBlockOperation in Objective-C, we would have to declare the variable as weak to be able to reference the block operation inside the block (to cancel if needed), typically like this: __weak NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{ if (blockOp.cancelled) { ... } }]; But in Swift, when I try declare my NSBlockOpeartion as weak, it is always nil. weak var blockOp = NSBlockOperation() Without the weak reference, all is fine except

How to make simultaneous https requests in Swift 3

有些话、适合烂在心里 提交于 2019-12-13 12:15:54
问题 I'm having problems to execute a https requests, if the request don't have any error i never get the message, this is a command line tool application and i have a plist to allow http requests, i always see the completion block. typealias escHandler = ( URLResponse?, Data? ) -> Void func getRequest(url : URL, _ handler : @escaping escHandler){ let session = URLSession.shared var request = URLRequest(url:url) request.cachePolicy = .reloadIgnoringLocalCacheData request.httpMethod = "GET" let

Core Data insert objects in NSOperation and sync

一笑奈何 提交于 2019-12-13 07:29:08
问题 I'm saving objects from network response in NSOperation . As I understand for merge MOC's changes from background threads to main I can use mergeChangesFromContextDidSaveNotification or performBlock with parent context. What should I prefer to use mergeChangesFromContextDidSaveNotification or performBlock ? What are the pros and cons of each merge method? NSOperation executed in background context. So when I call performBlock will be created new thread or not? 回答1: The answer depends very

NSOperation wait for event

三世轮回 提交于 2019-12-13 06:38:34
问题 I'd like to make a custom class that extends NSOperation in order to make successful communication with another device by bluetooth. The question I have is how can I implement the main method of the class so that it will wait for an event triggered by the bluetooth? 回答1: Never use an infinite loop. It is not energy-efficient. As @lead_the_zeppelin said, use semaphore/mutex/etc. For example you may use dispatch_group functions like this: dispatch_group_t waitGroup = dispatch_group_create();