By default NSMetaDataQuery results notifications are received on the main thread. It seems you have to call query.startQuery on the main thread, but you can use [query.setOperationQueue:] to set the queue to which results notifications will be sent.
Ideally I want a background thread to be receiving these results and processing them and depending on what files are found, I will set variables or post notifications to the main thread.
What I don't understand is how I create the operational …
我试图使用NSOperation和NSOperationQueue从某些服务器下载多个图像.我的主要问题是下面的代码片段和这个链接http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/之间的区别是什么?我更喜欢第二种解决方案,因为我们对操作有更多的控制,它更清洁,如果连接失败,你可以正确处理它.
如果我尝试使用下面的代码从服务器下载大约300张图片,我的应用程序将有一个相当大的延迟,如果我启动应用程序,并立即进入主屏幕,然后回到应用程序,我会崩溃,因为没有足够的时间让应用再次变得活跃.如果我取消注释[queue setMaxConcurrentOperationCount:1],则用户界面是响应的,如果它进入后台并返回到前台,它将不会崩溃.
但是如果我实现类似于上面链接的东西,我不需要担心设置maxConcurrentOperationCount,默认值很好.一切都是响应,没有崩溃,似乎所有队列都得到了更快的完成.
所以这让我想到第二个问题,为什么[queue setMaxConcurrentOperationCount:1]在我的代码中有如此大的影响?从文档中,我认为将maxConcurrentOperationCount保留为其默认值是正常的,这只是告诉队列根据某些因素决定最佳值应该是什么.
这是我在Stack Overflow上的第一篇文章,所以希望这是有道理的,感谢您的帮助!
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//[queue setMaxConcurrentOperationCount:1];
for(NSURL *URL in URLArray) {
[queue addOperationWithBlock:^{
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:&response error:&error];
if(!error && data) {
[data writeToFile:path atomically:YES];
}
}];
}
Run Code Online (Sandbox Code Playgroud) 如何获得NSOperationQueue的完成块,这里我想从所有操作的开始到结束旋转活动指示器.
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation, operation1, operation3,...] waitUntilFinished:NO];
Run Code Online (Sandbox Code Playgroud)
谢谢.
我的代码:
NSOperationQueue *queue;
-(void)viewDidLoad
{
queue = [NSOperationQueue new];
NSOperation* loadImgOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(refresh) object:nil];
[queue addOperation:loadImgOp];
}
-(void)refresh
{
[self operationFirst];
[self operationSecond];
...
[self operationFive];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[queue cancelAllOperations];
}
Run Code Online (Sandbox Code Playgroud)
当我调用 new ViewController 时,-(void)refresh 继续工作。cancelAllOperations 不起作用。
我对 NSOperation(现在称为操作)进行了子类化,以在后台执行一些异步查询。我希望一次执行一个。为此,我将 maxConcurrentOperationCount 设置为 1,但它仍然允许队列中存在多个对象。
我已经在类声明的正下方声明了我的队列:
let downloadQueue = OperationQueue()
Run Code Online (Sandbox Code Playgroud)
然后在视图中加载设置了计数:
downloadQueue.maxConcurrentOperationCount = 1
Run Code Online (Sandbox Code Playgroud)
然后在代码中调用Operation子类:
self.downloadQueue.addOperation(DownloadOperation(col: collectionView, index: indexPath, cell: cell))
Run Code Online (Sandbox Code Playgroud)
还有我的子类:
class DownloadOperation : Operation {
var collectionView: UICollectionView
var indexPath: IndexPath
var collectionCell: InnerCollectionCell
init(col: UICollectionView, index: IndexPath, cell: InnerCollectionCell) {
collectionView = col
indexPath = index
collectionCell = cell
}
let mainQueue = OperationQueue.main
override func main() {
if(isCancelled) {
return
}
///
/// Long query that gets objects in background
/// it is itself an async …Run Code Online (Sandbox Code Playgroud) iOS 13progress在OperationQueue类中引入了该属性。同时,Apple 将operations和operationCount属性标记为已弃用,这表明它们不应再用于报告队列进度。
我的问题是我无法让该progress属性按照我期望的方式工作(这基本上是开箱即用的)。此外,我找不到有关此新属性的任何文档(除了它现在存在的文档)。
我试图让它在一个新的 SingleView 项目中工作,该项目UIProgressView在 main 上有一个UIViewController。该示例深受https://nshipster.com/ios-13/的启发。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var progressView: UIProgressView!
private let operationQueue: OperationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.underlyingQueue = .global(qos: .background)
return queue
}()
override func viewDidLoad() {
super.viewDidLoad()
self.progressView.observedProgress = operationQueue.progress
self.operationQueue.cancelAllOperations()
self.operationQueue.isSuspended = true
for i in 0...9 {
let operation = BlockOperation {
sleep(1)
NSLog("Operation …Run Code Online (Sandbox Code Playgroud) 每个人Thread都有自己的RunLoop,如何DispatchQueue与他们互动?是DispatchQueue使用RunLoop分派任务Thread还是以其他方式完成?
nsoperationqueue grand-central-dispatch runloop swift dispatch-queue
我很确定这是100%安全的,但我不想错过任何东西.我有以下代码
- (void) scheduleControlSurfaceProcess {
[self.operationQueueForMessageProcessing addOperationWithBlock:^{
// do something
[self scheduleControlSurfaceProcess];
}];
}
Run Code Online (Sandbox Code Playgroud)
自我是单身人士.该块作为非主线程线程非常出色.我没有在profiler中看到任何内存问题(我不太相信).
那么,我可以忽略这个警告,"Block会被捕获对象强烈保留的对象保留吗?" 如果没有,我怎么能坚持要释放块(用ARC)?让警告消失很容易,但分配id what = self似乎无法解决问题.
编辑:正如我在这个问题中意识到的那么晚,这里真正的问题是我从块内部重新安排.这显然是有问题的,因为每个块保留下一个.
nsoperationqueue ios objective-c-blocks automatic-ref-counting
我一直在搜索,但只能找到委托模式的想法来传递来自NSOperation的数据.我有一个NSOperation,在完成NSOperation后下载数据我希望它能够传递给将它下载的数据放入NSoperationQueue的类.在我的队列中最多可以有100个这样的NSOPerations,它们都检索唯一的数据.任何想法将不胜感激.
我正在开发一个应用程序,当它开始执行时,它必须从webService获取一些数据,类别,加载图像(有时会更改),信息"如何使用"(也可以在服务器中更改,客户端规范.. ).为了获得这些数据,我调用了一些像这样的方法(我有四个类似的方法,每个我需要的一个方法):
-(void) loadAppInfo
{
__weak typeof(self) weakSelf = self;
completionBlock = ^(BOOL error, NSError* aError) {
if (error) {
// Lo que sea si falla..
}
[weakSelf.view hideToastActivity];
};
[self.view makeToastActivity];
[wpNetManager getApplicationInfoWithCompletionBlock:completionBlock];
}
Run Code Online (Sandbox Code Playgroud)
在我的网络管理器中,我有类似这样的方法:
- (void)getApplicationInfoWithCompletionBlock:(CompletionBlock)completionBlock
{
NSString * lang = @"es";//[[NSLocale preferredLanguages] objectAtIndex:0];
NSString *urlWithString = [kAPIInfoScreens stringByAppendingString:lang];
NSMutableURLRequest *request = nil;
request = [self requestWithMethod:@"GET" path:urlWithString parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[self registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response …Run Code Online (Sandbox Code Playgroud) objective-c nsoperationqueue grand-central-dispatch ios objective-c-blocks
nsoperationqueue ×10
ios ×7
nsoperation ×4
swift ×3
objective-c ×2
ios13 ×1
iphone ×1
progress-bar ×1
runloop ×1