来自文档:
操作对象一次最多只能有一个操作队列,如果操作已经在另一个队列中,则此方法抛出NSInvalidArgumentException异常.同样,如果操作当前正在执行或已经完成执行,则此方法抛出NSInvalidArgumentException异常.
那么如何检查我是否可以安全地将NSOperation添加到队列中?
我知道的唯一方法是添加操作,然后尝试捕获异常,如果操作已经在队列中或之前执行过.
我正在尝试在特定函数中实现异步URL请求,我希望所有这些请求完成然后执行特定操作但操作先于请求,即在请求完成之前调用它.
dispatch_queue_t fetchQ = dispatch_queue_create("Featured Doc Downloader", NULL);
dispatch_async(fetchQ, ^{
[self myAsyncMultipleURLRequestFunction];
dispatch_sync(dispatch_get_main_queue(), ^{
[self updateUIFunction];
});
});
-(void)myAsyncMultipleURLRequestFunction
{
for (int i=0; i<count; i++)
{
NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
}
Run Code Online (Sandbox Code Playgroud)
现在在myAsyncMultipleURLRequestFunction完成所有请求之前调用updateUIFunction.也尝试使用NSOperaitonQueue,但不能做我真正想要的.
[_operationQ addOperationWithBlock: ^ {
for (int i=0; i<count; i++)
{
NSURLConnection *loginConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}
}
[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
// updating UI
[self updateUIFunction];
}];
}];
Run Code Online (Sandbox Code Playgroud)
我知道这很简单,但我的时间不多了,任何帮助都表示赞赏.
来自文档:
当isFinished方法返回的值更改为YES时,将执行您提供的完成块.因此,在操作的主要任务完成或取消之后,操作对象执行该块.
我正在使用RestKit/AFNetworking,如果重要的话.
我NSOperation在a中有多个依赖项OperationQueue.我使用完成块来设置我的孩子需要的一些变量(将结果附加到数组).
(task1,...,taskN) - > taskA
taskA addDependency:task1-taskN
taskA是否会收到不完整的数据,因为孩子可以在完成块被触发之前执行?
参考
NSOperations及其completionBlocks同时运行吗?
我通过在完成块中添加一个睡眠进行了简单的测试,结果不一样.完成块在主线程中运行.当所有完成块都处于休眠状态时,子任务就会运行.
objective-c nsoperationqueue objective-c-blocks restkit afnetworking
我真的需要帮助.我在这一点上很绝望.
我有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) 我正在使用NSOPerationqueue开发应用程序.它显示我在队列中添加OperationBlock时泄漏,如下图所示.请帮我找出泄漏分辨率.如果您需要更多屏幕截图或细节请告诉我.
您还可以在图像中查看我的代码.



是的我知道.关于这个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
我通过创建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不会改变任何事情.也许在内部,网络请求以不同的方式序列化?我如何确定这些请求的优先顺序?
提前致谢.
我有一个应用程序,用户可以按顺序下载多个文件。我按照罗布先生的解决方案进行顺序下载。但是,当我尝试取消下载时遇到问题。
当我尝试取消下载时,有两种情况。
这是代码
下载管理器.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) 你好呀,
在我的应用程序中,我创建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 的新手,我想知道是否还有其他方法可以实现我想做的事情?
提前致谢 !
我有一个NSOperationQueue,maxConcurrentOperationCount = 1,
和自定义NSOperation有一个属性是块,
当iOS 9.x中的NSOperationQueue addOperation,应用程序崩溃时,
NSOperation不是零.
以下是Apple崩溃报告,但我不能再出现:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x1000000013ed199e
0 Foundation 0x0000000182f3b4e8 ____addOperations_block_invoke607 + 524 (NSOperation.m:193)
1 Foundation 0x0000000182f3b488 ____addOperations_block_invoke607 + 428 (NSOperation.m:204)
2 Foundation 0x0000000182f26170 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:usingBlock:] + 672 (NSKeyValueObserving.m:2406)
3 Foundation 0x0000000182e7afc0 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 64 (NSKeyValueObserving.m:2432)
4 Foundation 0x0000000182f392e4 __addOperations + 1400 (NSOperation.m:2100)
Run Code Online (Sandbox Code Playgroud)
谢谢!
nsoperationqueue ×10
nsoperation ×7
ios ×6
iphone ×3
objective-c ×3
afnetworking ×2
restkit ×2
asynchronous ×1
crash ×1
memory-leaks ×1
swift ×1