标签: nsoperationqueue

NSOperationQueue内部的NSOperation未被执行

我真的需要帮助.我在这一点上很绝望.

我有NSOperation,当添加到NSOperationQueue时没有被触发.我添加了一些日志记录以查看NSOperation状态,结果如下:

Queue operations count = 1
Queue isSuspended = 0
Operation isCancelled? = 0
Operation isConcurrent? = 0
Operation isFinished? = 0
Operation isExecuted? = 0
Operation isReady? = 1
Operation dependencies? = 0
Run Code Online (Sandbox Code Playgroud)

代码很简单.没什么特别的.

       LoadingConflictEvents_iPad *loadingEvents = [[LoadingConflictEvents_iPad alloc] initWithNibName:@"LoadingConflictEvents_iPad" bundle:[NSBundle mainBundle]];

       loadingEvents.modalPresentationStyle = UIModalPresentationFormSheet;
       loadingEvents.conflictOpDelegate = self;

       [self presentModalViewController:loadingEvents animated:NO];

       [loadingEvents release];

       ConflictEventOperation *operation = [[ConflictEventOperation alloc] initWithParameters:wiLr.formNumber pWI_ID:wiLr.wi_id];

       [queue addOperation:operation];           

       NSLog(@"Queue operations count = %d",[queue operationCount]);
       NSLog(@"Queue isSuspended = %d",[queue isSuspended]);
       NSLog(@"Operation isCancelled? = %d",[operation isCancelled]); …
Run Code Online (Sandbox Code Playgroud)

nsoperation nsoperationqueue ios

5
推荐指数
2
解决办法
6649
查看次数

揭开NSOperation的神秘面纱:并发与非并发和异步模式

是的我知道.关于这个NSOperation世界有很多问题和答案,但我仍然有一些疑问.我试图用两部分问题来解释我的怀疑.他们彼此相关.

在SO后nsoperationqueue和并发-VS-非并发,达伦写道:

"并发"操作本身是并发的; 它不需要NSOperationQueue为它创建一个线程.

但是稍微搜索一下,我发现a NSOperation,即使它被声明为并发(通过覆盖isConcurrent它返回的方法YES),也可以添加到NSOperationQueue.这是什么意思?如果我将一个并发NSOperation队列添加到队列中,那么幕后发生了什么?相反,如果我按原样使用并发操作(不将其添加到队列中)会发生什么?

从Apple doc获取的说明很清楚:

...操作队列忽略isConcurrent返回的值,并始终从单独的线程调用操作的start方法....通常,如果您始终使用具有操作队列的操作,则没有理由使它们并发.

然后,我真的对使用异步模式感兴趣NSOperation.我找到了Dave Dribin的一个很好的教程(并发操作).我得到了他的帖子的整体意义.

您无法使用异步模式(例如,使用异步NSURLConnection请求),因为无法调用委托.当main完成该操作将被删除.解决方案是覆盖start控制操作生命周期的方法......处理运行循环可能会很痛苦.

现在,试图理解他的帖子,我的疑问是需要start在主线程中运行该方法.

- (void)start
{
    if (![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }

    // other code here...
}
Run Code Online (Sandbox Code Playgroud)

在处理异步API时,我们可以在启动时在主线程上开始异步调用,并使操作保持运行直到完成.

你能解释一下为什么吗?

先感谢您.

asynchronous objective-c nsurlconnection nsoperation nsoperationqueue

5
推荐指数
1
解决办法
3819
查看次数

UIImageView + AFNetworking映像请求队列阻止来自RestKit的其他网络请求

我通过创建RKObjectRequestOperations并将它们添加到RestKit的队列来下载Data with RestKit :

RKObjectRequestOperation *operation = [RK objectRequestOperationWithRequest:request
       success:...
       failure:...];
[RK enqueueObjectRequestOperation:operation];
Run Code Online (Sandbox Code Playgroud)

那个效果很好.此外,此数据显示在列表视图中,其中包含显示相关用户图标的UIImageViews.但是,这些用户图标不是通过RestKit下载,而是通过底层的AFNetworking库下载.UIImageView + AFNetworking也做得很好(包括缓存):

[self setImageWithURLRequest:userIconRequest
            placeholderImage:anonymousUser
                     success:nil
                     failure:...];
Run Code Online (Sandbox Code Playgroud)

问题是来自图像视图的那15个用户图标请求阻止了应该立即加载下一页的RestKit请求的处理.我可以看到显示"加载"行的列表以及第一个用户图标.在最后一张图片完成加载的那一刻,下一页自己附加.

查看UIImageView + AFNetworking的实现表明它正在使用自己的序列化请求的NSOperation队列实例.但是,我认为这不应该干扰RestKit.

同时向NSOperationQueuePriority所有请求添加s不会改变任何事情.也许在内部,网络请求以不同的方式序列化?我如何确定这些请求的优先顺序?

提前致谢.

iphone nsoperationqueue ios restkit afnetworking

5
推荐指数
1
解决办法
620
查看次数

UITableView reloadData很慢

我有一个连接到互联网的功能,然后刷新表格中的单元格.

我的功能是:

- (void) updateMethod
{

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.name = @"Data request queue";

    [queue addOperationWithBlock:^{

        //neither of these delay the responsiveness of the table
        [columnArrayBackground removeAllObjects];
        [self getColumnDataBackground];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            for (int i=0; i < 6; i++) {
                columnArray[i] = columnArrayBackground[i];
            };

            //THIS ONE LINE CAUSES DELAYS
            [homeTable reloadData];


        }];
    }];
}
Run Code Online (Sandbox Code Playgroud)

除了[homeTable reloadData]之外,一切都超级快.当我发表评论时,我得到快速反应.当我取消注释时,我的细胞反应有时会滞后几秒钟!

我的其他reloadData调用不会延迟我的应用程序.我没有正确实现NSOperationQueue吗?

performance objective-c uitableview nsoperationqueue ios

5
推荐指数
1
解决办法
4582
查看次数

NSOperationQueue如何等待两个异步操作?

如何让NSOperationQueue(或其他任何东西)等待带有回调的两个异步网络调用?流程需要看起来像这样

Block Begins {
    Network call with call back/block begins {
        first network call is done 
    }
}
Second Block Begins {
    Network call with call back/block begins {
        second network call is done 
    }
} 

Only run this block once the NETWORK CALLS are done {
    blah
}
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的.

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
__block NSString *var;


[queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
   [AsyncReq get:^{
       code
    } onError:^(NSError *error) {
       code
    }];
}]];

[queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
   [AsyncReq get:^{
       code
    } onError:^(NSError …
Run Code Online (Sandbox Code Playgroud)

nsoperationqueue ios

5
推荐指数
2
解决办法
4123
查看次数

如何使用 Swift 取消队列中的下载

我有一个应用程序,用户可以按顺序下载多个文件。我按照罗布先生的解决方案进行顺序下载。但是,当我尝试取消下载时遇到问题。

当我尝试取消下载时,有两种情况。

  1. 我想取消当前下载的文件。当我取消该文件时,下载可以继续到队列中的下一个文件
  2. 我想取消当前在队列中的文件。队列有cancelAll()方法,它将取消队列中的所有文件。

这是代码

下载管理器.swift

class DownloadManager: NSObject, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate
{

    /// Dictionary of operations, keyed by the `taskIdentifier` of the `NSURLSessionTask`
    internal var delegate : DownloadVC!
    private var operations = [Int: DownloadOperation]()

    /// Serial NSOperationQueue for downloads

    let queue: NSOperationQueue = {
        let _queue = NSOperationQueue()
        _queue.name = "download"
        _queue.maxConcurrentOperationCount = 1
        return _queue
    }()

    /// Delegate-based NSURLSession for DownloadManager

    lazy var session: NSURLSession = {
        let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
        return NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: …
Run Code Online (Sandbox Code Playgroud)

nsoperation nsoperationqueue ios swift

5
推荐指数
1
解决办法
2984
查看次数

从 NSOperationQueue 获取特定的 NSOperation

你好呀,

在我的应用程序中,我创建NSOperations并将它们添加到NSOperationQueue. 有时,我想从操作队列中取消某些特定操作,这就是为什么我为 NSOperation 子类定义了一个标识符属性:

@property (nonatomic, assign) NSString *identifier;
Run Code Online (Sandbox Code Playgroud)

但是,当我循环进入队列的操作并且想要将操作的标识符与我想要获取的操作的标识符进行比较时,我得到了一个EXC_BAD_ACCESS指出 if 条件的提示:

for (MyCustomNSOperationClass *operation in self.myOperationQueue.operations)
{
     NSString *identifier = [self getRelatedIdentifier];
     if ([operation.identifier isEqualToString:identifier])
     {
           [operation cancel];
     }
}
Run Code Online (Sandbox Code Playgroud)

操作的标识符应该类似于33a37fb0-8f77-0132-6c0b-5254005d9147但当它崩溃时,它类似于0x7be4af00(当我使用时po operation.identifier)。我说当它崩溃时,因为它并不总是崩溃,当它不崩溃时,那么标识符是正确的(我不确定是否非常清楚......)。

由于我是 NSOperation 的新手,我想知道是否还有其他方法可以实现我想做的事情?

提前致谢 !

nsoperation nsoperationqueue ios

5
推荐指数
1
解决办法
981
查看次数

将主队列设置为 NSOperationQueue 的底层队列

NSOperationQueueclass 有一个underlyingQueue属性,用于设置将在其NSOperation上执行实例的调度队列。

let dispatchQueue = dispatch_queue_create("custom.queue", DISPATCH_QUEUE_SERIAL)
let opQueue = NSOperationQueue()
opQueue.underlyingQueue = dispatchQueue
Run Code Online (Sandbox Code Playgroud)

但是,官方文档指出:

此属性的值不能是由 dispatch_get_main_queue

苹果似乎没有更多关于这个问题的解释。但是,使用主队列 asunderlyingQueue不会引发任何错误,也不会产生任何不需要的行为。这背后的原因是什么?

nsoperation nsoperationqueue ios swift

5
推荐指数
1
解决办法
769
查看次数

使用带有组合框架的操作队列

随着combine框架的到来,是不是还需要用到操作队列呢?例如,苹果在 WWDC 应用程序中几乎无处不在地使用操作队列。那么如果我们使用 SwiftUI 和 combine(异步编程),是否需要使用操作队列?

nsoperationqueue swiftui combine

5
推荐指数
1
解决办法
947
查看次数

即使设置了操作的优先级和依赖关系,操作队列也不会按顺序执行

我正在进行三个 api 调用,并希望 API1 应首先执行,完成后应执行 API2,然后执行 API3。我为此使用了操作队列,并添加了对操作的依赖性。我也尝试设置优先级,但没有按顺序获取 api 调用。帮我看看如何正确制作。

代码是这样的:

let op1 = Operation()
op1.completionBlock = {
    self.APICall(urlString: self.url1)
}
op1.queuePriority = .veryHigh

let op2 = Operation()
op2.completionBlock = {
    self.APICall(urlString: self.url2)
}
op2.queuePriority = .high

let op3 = Operation()
op3.completionBlock = {
    self.APICall(urlString: self.url3)
}

op3.queuePriority = .normal

op2.addDependency(op1)
op3.addDependency(op2)

queue.addOperations([op1, op2, op3], waitUntilFinished: false)
Run Code Online (Sandbox Code Playgroud)

我将 API 调用方法放在 DispatchQueue.main.sync 中,如下所示:

func APICall(urlString: String) {

    let headers: HTTPHeaders = [
        "Accept": "text/html"
    ]
    print(urlString)
    DispatchQueue.main.sync {

        Alamofire.request(urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!, method: .get, …
Run Code Online (Sandbox Code Playgroud)

queue nsoperationqueue ios swift

5
推荐指数
1
解决办法
1892
查看次数