标签: nsoperationqueue

NSOperations的队列和处理应用程序退出

我即将创建一系列NSOperations并将它们运行在队列中.

它们都是顺序的并且一次运行一个.

这些操作将从Web获取数据并创建和保存核心数据管理对象.

如何处理应用程序退出的情况?由于操作在分离线程中运行,如何让主线程等到当前操作"安全"退出?在某些情况下,我很高兴线程(操作)在完成之前退出,因为在进一步的应用程序启动时,作业将继续并从中断处继续.

非常感谢,

麦克风

iphone multithreading core-data nsoperation nsoperationqueue

4
推荐指数
1
解决办法
2133
查看次数

iPhone:NSOperationQueue连续运行操作

我有一个单独的NSOperationQueue来处理我的所有网络请求.但是,我注意到,当我运行一个特别长的操作时(此特定操作至少需要25秒),我的其他操作在完成之前不会运行.

maxConcurrentOperationCount设置为NSOperationQueueDefaultMaxConcurrentOperationCount,所以我不相信这是问题所在.

有什么理由会发生这种情况吗?除了产生多个NSOperationQueues(一个我不确定会起作用的解决方案,我也不确定它是个好主意),解决这个问题的最佳方法是什么?

谢谢.

concurrency multithreading nsoperation nsoperationqueue

4
推荐指数
1
解决办法
2107
查看次数

取消NSOperation for for循环?

我正在尝试使用NSOperationon 在后台线程上实现搜索iOS.我不想继承子类,NSOperation所以这就是我正在做的事情:

[searchQueue cancelAllOperations];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self
                                                                  elector:@selector(filterContentForSearchText:)
                                                                   object:self.searchDisplayController.searchBar.text];
[searchQueue addOperation:op];
[op release];
Run Code Online (Sandbox Code Playgroud)

搜索方法包括for循环,用于检查正在搜索的内容是否在数组中.现在,当我取消NSOperation通过调用时cancelAllOperations,for循环继续在数组中运行.我想阻止这一点,并想知道在for循环中调用它是否合法:

if ([[[searchQueue operations] objectAtIndex:0] isCancelled]) {
    [tmp_array release];   // tmp_array is used to hold temporary results
    [pool drain];          // pool is my autorelease pool
    return;
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsoperation nsoperationqueue ios

4
推荐指数
1
解决办法
2418
查看次数

使NSOperation同步

我正在开发一个应用程序,允许用户从iPad上创建云上的文件和文件夹.删除文件后,应用程序会自动在云上创建"回收站"文件夹并将该文件放入其中.我已NSOperationQueue为所有操作创建,即我有单独的文件夹创建队列和单独的文件上载队列.我面临的问题是,文件上传操作在文件夹创建操作完成之前执行,因此文件无法成功上传.

任何人都可以帮助我使文件夹创建操作同步吗?

我试过以下代码

[create_folder_queue addOperations:[NSArray arrayWithObject:folderOperation] waitUntilFinished:YES];
Run Code Online (Sandbox Code Playgroud)

但它不执行操作.

提前致谢.

iphone nsoperation nsoperationqueue ios

4
推荐指数
1
解决办法
6910
查看次数

在辅助线程中设置NetworkActivityIndi​​catorVisible值是否安全?

我想知道AppDelegate是否是线程安全的?我目前有一个在辅助线程上运行网络任务的操作,当任务开始时,我想设置NetworkActivityIndicatorVisibleYES,当任务完成时,将其设置为NO.我是否必须始终在主线程中调用它,或者我可以在当前的运行循环线程中执行它?

谢谢

objective-c nsoperation nsoperationqueue ios

4
推荐指数
1
解决办法
1130
查看次数

我怎么知道nsoperation队列何时完成了所有请求,以便我可以重新加载我的tableview?

我正在使用ASIHTTPRequest和NSOperationQueue从不同的链接下载数据

在后台线程下载.请求完成后,我使用requestFinished进行解析

ASIHTTPRequest的委托方法.我希望在所有请求中更新tableview中的数据

队列已经完成.有没有办法知道NSOperationQueue何时处理完所有

要求?我的意思是队列有任何变量,如'isEmpty'或任何委托方法,如'queueDidCompletedAllOperation'?

请帮忙.

这是代码:

//source

@interface SourceModel : NSObject

@property (nonatomic, retain) NSString * link;

@property (nonatomic, retain) NSString * name;

@end


//for rssGroup

@interface CompleteRSSDataModel : NSObject

@property (nonatomic,strong) SourceModel * source;

@property (nonatomic,strong) KissXMLParser * parser;

@property (nonatomic,strong) NSArray * rssArticles;

@end

- (void)viewDidLoad
{
       for (int index=0; index<[rssGroups count]; index++) {

            NSString * urlString = [[[rssGroups objectAtIndex:index] source] link];

            NSURL *url = [NSURL URLWithString:urlString];

            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self];

            //set this request's …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c asihttprequest nsoperationqueue ios5

4
推荐指数
1
解决办法
7626
查看次数

NSOperationQueue和后台支持

我在制定有用的策略以支持NSOperationQueue课堂背景时遇到了一些问题.特别是,我有一堆NSOperations执行以下操作:

  • 从网上下载文件
  • 解析文件
  • 在Core Data中导入数据文件

操作将插入到串行队列中.一旦操作完成,下一个操作就可以开始了.

当应用程序进入后台时,我需要停止(或继续)操作.从这些讨论(AFNetworking是否有后台支持?NSOperations的队列以及处理应用程序退出)我看到最好的方法是取消操作并isCancelled在每个操作中使用属性.然后,检查针对该属性的操作的关键点,它允许在应用程序进入后台时回滚执行(运行操作的)的状态.

基于突出背景支持的Apple模板,我该如何管理类似情况?我可以简单地取消操作或等待当前操作完成吗?请参阅评论了解详情

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

        // Do I have to call -cancelAllOperations or
        // -waitUntilAllOperationsAreFinished or both?

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // What about here?

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}
Run Code Online (Sandbox Code Playgroud)

先感谢您.

编辑

如果NSOperation main方法执行下面的代码,如何遵循响应取消事件模式?

- (void)main
{
    // 1- download …
Run Code Online (Sandbox Code Playgroud)

background nsoperation nsoperationqueue ios

4
推荐指数
1
解决办法
7549
查看次数

不使用ARC时替换__weak

我有这行代码:

__weak NSBlockOperation *weakOperation = operation;
Run Code Online (Sandbox Code Playgroud)

这是触发此编译器错误:

__weak attribute cannot be specified on automatic variable.
Run Code Online (Sandbox Code Playgroud)

原因是我没有启用ARC(尚未准备好进行切换).所以从另一个StackOverFlow问题,我被建议使用:

__unsafe_unretained NSBlockOperation *weakOperation = operation; 
Run Code Online (Sandbox Code Playgroud)

这使得错误消失,但对于我正在使用它的上下文,它不起作用(如果感兴趣,请参阅此问题:如何取消NSOperationQueue).

所以我的问题是,__weak在这个实例中我可以用关键字代替什么来摆脱这个警告?当我使用时__weak,一切都能正常工作,但我担心它不会影响iOS的未来版本.

cocoa-touch nsoperationqueue ios objective-c-blocks retain-cycle

4
推荐指数
1
解决办法
2160
查看次数

在取消请求时NSOperation从NSOperationQueue中移除的确切时间?

我正在阅读一些文档,解释如何在NSOperationQueue中管理NSOperation.我的重点是,如果用户在进度面板中按下取消按钮或退出应用程序,则根本不执行操作.因此,取消操作以防止它不必要地消耗CPU时间.

因此,每当我需要取消操作时,我应该触发取消方法以防止进一步执行.然后我将不得不定期使用操作对象状态isCancelled来检查操作是否被取消.以下是我对此的疑问:

(1)在取消请求时,如果从NSOperationQueue中移除NSOperation,那么我们如何仍然引用该NSOperation及其属性被取消

根据Apple Developer Class Reference:

NSOperationQueue类调节一组NSOperation对象的执行.添加到队列后,操作将保留在该队列中,直到明确取消或完成其任务为止.

(2)如果我使用ARC,我是否需要关心取消请求?我举个例子.我有2个视图控制器A和B.在B中,我使用NSOperation制作8到10个NSURLRequest并将所有请求放在NSOperationQueue中.这里NSOperationQueue的对象是视图控制器B的属性.因此,如果用户按下后退按钮返回到视图A,在ARC下,NSOperationQueue的对象应该被自动删除(当我弹出以查看A时).ARC机制是否会取消所有操作,还是应该有一些机制来避免无用的执行?

objective-c nsoperation nsoperationqueue ios automatic-ref-counting

4
推荐指数
1
解决办法
1482
查看次数

NSOperationQueue addOperations waitUntilFinished

嗨,我正在使用Swift构建一个应用程序.我需要按特定顺序处理通知.因此我尝试使用addOperations waitUntilFinished.

这是我做的:

let oldify = NSOperation()
    oldify.completionBlock = {
println("oldify")
}
let appendify = NSOperation()
    appendify.completionBlock = {
println("appendify")
}
let nettoyify = NSOperation()
    nettoyify.completionBlock = {
println("nettoyify")
}
NSOperationQueue.mainQueue().maxConcurrentOperationCount = 1
NSOperationQueue.mainQueue().addOperations([oldify, appendify, nettoyify], waitUntilFinished: true)
Run Code Online (Sandbox Code Playgroud)

使用此代码,没有任何操作正在执行.当我尝试这样做时:

NSOperationQueue.mainQueue().maxConcurrentOperationCount = 1
NSOperationQueue.mainQueue().addOperation(oldify)
NSOperationQueue.mainQueue().addOperation(appendify)
NSOperationQueue.mainQueue().addOperation(nettoyify)
Run Code Online (Sandbox Code Playgroud)

操作执行但不按正确的顺序执行.

有谁知道我做错了什么?我对NSOperations迅速而又全新地充满信心

nsoperation nsoperationqueue ios swift

4
推荐指数
1
解决办法
4551
查看次数