我正在创建一个MyOperation对象(继承自NSOperation)并添加到NSOperationQueue.然后我在MyOperation上做KVO.我正在使用这种方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
Run Code Online (Sandbox Code Playgroud)
如果完成,从MyOperation获取值.在这个方法中,我使用其他类的方便方法来获取其他数据.
也许这里的observeValue ...方法有同步问题吗?
我试图弄清楚为什么在添加到NSOperationQueue(iOS5,ARC)时没有执行具有依赖关系的NSOperation:
@implementation NSOperationTest {
NSOperationQueue *_operationQueue;
}
- (id)init {
self = [super init];
if (self) {
_operationQueue = [[NSOperationQueue alloc] init];
}
return self;
}
-(void) test
{
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op1 running");
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op2 running");
}];
[op2 addDependency:op1];
[_operationQueue addOperation:op2];
}
@end
Run Code Online (Sandbox Code Playgroud)
这让我抓狂,这里op1应该先执行op2,但要么执行,要么在没有依赖的情况下添加都可以正常工作.有人知道为什么吗?
提前致谢.
谁能给我之间的完美差异的说明NSOperationQueue和NSAutoReleasePool
我已经从网址下载了信息,我将这个网址作为NSOperation在NSOperationQueue中发送,我想知道,如何删除特定的NSOperation以下载特定网址的数据,现在我这样做:
AppDelegate *appController = (AppDelegate *) [[UIApplication sharedApplication] delegate];
for (NSOperation *op in appController.seriesQueue.operations) {
if ([op isKindOfClass:[MyDowndload class]]) {
MyDownload *urlInDownload = (MyDowndload *)op;
if ([urlInDownload.myNew.urlName isEqualToString:[managedObject valueForKey:@"urlName"]] && [urlInDownload.myNew.language isEqualToString:[managedObject valueForKey:@"language"]]) {
[op cancel];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在tableview中有信息,所以当我为索引路径删除一行时,我输入此检查,并在[op cancel]行中输入,但我可以在控制台日志中看到该线程仍在下载,如何我能停下来删除吗?
以下代码会在 iOS 模拟器中产生崩溃。
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
NSDate *sleepStart = [NSDate date];
while ([sleepStart timeIntervalSinceNow] > -300) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
更新:即使在后台线程上也会出现此问题。
下面的代码也有错误:
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDate *sleepStart = [NSDate date];
while ([sleepStart timeIntervalSinceNow] > -300) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
就这样。将其粘贴到任何视图控制器中,在模拟器中运行该应用程序正好 4 分 15 秒,它就会崩溃。
这种崩溃是我以前从未见过的。是“EXC_???(11)”。奇怪的是,您可以在崩溃后按“继续”按钮,它会像平常一样继续。
为什么会崩溃?如何将长任务提交到队列而不导致此行为?
到目前为止,我尝试过以下一些方法,但还没有解开这个谜团:
更新#1
该问题仅在 LLDB 下重现,在 …
我正在使用enqueueBatchOfHTTPRequestOperations提交一批请求.如果任何请求失败,我想立即取消任何其他仍在进行的请求.为此,我在各个操作上设置故障回调来执行操作[client.operationQueue cancelAllOperations];.
这似乎取消了所有剩余的操作,但它也阻止了批处理的整个completionBlock执行...这是我试图测试此行为的代码(其中一个请求总是在服务器上设置为失败).
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://arahlf.com"]];
NSMutableArray *requests = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"echo.php" parameters:@{ @"sleep": @(i) }];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:nil failure:nil];
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request failed, cancelling all operations.");
[client.operationQueue cancelAllOperations];
}];
[requests addObject:operation];
}
[client enqueueBatchOfHTTPRequestOperations:requests progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"Progress: %i/%i", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All …Run Code Online (Sandbox Code Playgroud) 我想执行多个 Alamofire 请求。但是,由于数据依赖性,新请求应仅在前一个请求完成时开始。
我已经用更一般的异步请求示例提出了一个问题,该示例使用OperationQueue. 但是,我没有成功地使用 Alamofire 实现相同的目标。
public func performAlamofireRequest(_ number: Int, success: @escaping (Int) -> Void)->Void {
Alamofire.request(String(format: "http://jsonplaceholder.typicode.com/posts/%i", number+1)) // NSURLSession dispatch queue
.responseString { response in // Completion handler at main dispatch queue?
if response.result.isSuccess {
// print("data")
} else if response.result.isFailure {
// print("error")
}
success(number) // Always leave closure in this example
}
}
Run Code Online (Sandbox Code Playgroud)
为了确保在下一个请求开始之前完成请求,我使用OperationQueue如下:
let operationQueue = OperationQueue.main
for operationNumber in 0..<4 { // Create some operations
let …Run Code Online (Sandbox Code Playgroud) 使用operation.cancel()不会取消当前操作.如果队列中有操作,它似乎取消它,但如果它正在执行,那么它似乎不会停止它.
我是否需要将某些内容发送回我的子类操作中的main()函数以使其停止?
for operation in downloadQueue.operations {
if operation.name == opName {
if operation.isExecuting == true {
operation.cancel()
}
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到了崩溃,在 Crashlytics 中报告,我不知道如何重现错误,它是随机发生的,所以很难用 Xcode 调试它。有任何想法吗?
Crashed: NSOperationQueue 0x280419200 (QOS: UNSPECIFIED)
0 libobjc.A.dylib 0x22c471430 objc_retain + 16
1 CoreFoundation 0x22d2b5888 __CFBasicHashAddValue + 1480
2 CoreFoundation 0x22d1e64ac CFDictionarySetValue + 260
3 Foundation 0x22dd04888 _encodeObject + 732
4 myAPI 0x1062b44b0 -[DataCore encodeWithCoder:] (DataCore.m:236)
5 myAPI 0x1062909c4 -[DataHandle encodeWithCoder:] (DataHandle.m:53)
6 Foundation 0x22dd04aa8 _encodeObject + 1276
7 Foundation 0x22dc69c6c +[NSKeyedArchiver archivedDataWithRootObject:] + 168
8 myAPI 0x106288a34 __77+[CachableObject addObjectToCache:withCacheName:withTTL:withCompletionBlock:]_block_invoke (CachableObject.m:162)
9 Foundation 0x22dd198bc NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 16
10 Foundation 0x22dc21ab8 -[NSBlockOperation main] + 72
11 Foundation …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用NSOperationQueue,我想为我的操作设置多个参数,我该怎么做?
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall) object:nil];
[queue addOperation:operation];
[operation release];
Run Code Online (Sandbox Code Playgroud) iphone cocoa-touch nsoperation nsoperationqueue nsinvocationoperation
nsoperationqueue ×10
nsoperation ×6
iphone ×5
cocoa-touch ×3
ios ×3
objective-c ×3
swift ×2
afnetworking ×1
alamofire ×1
crash ×1
crashlytics ×1
ios5 ×1