标签: nsoperationqueue

NSInvocationOperation和主线程

想象一下,我有一个视图,其中有一些UIKit对象作为其子视图(例如,UIActivityIndicatorView- 这无关紧要).此视图还有一个名为的选择器,doSomething它以某种方式管理UIKit对象(在我们的示例中,它可以启动或停止指示器视图).

我创建NSInvocationOperation(从视图的代码部分)initWithTarget:self selector:@selector(doSomething) object:nil.然后将其添加到NSOperationQueue.一切正常.

怎么样?!它应该是一个新的线程和非线程安全的UIKit对象!为什么没有发现错误(没有发生崩溃)?

iphone objective-c thread-safety nsoperationqueue

2
推荐指数
1
解决办法
3698
查看次数

将NSOperation添加到启动异步ASIHTTPRequest的NSOperationQueue

因此,我正在尝试执行所有在后台线程上下载数据的REST调用,以便UI保持响应.

我有一个包含NSOperationQueue的viewcontroller.我创建了一个我的导入器类的实例,它是NSOperation的子类.在我的导入器的main()方法中,我正在设置ASIHTTPDataRequest.我创建了请求,然后是开始请求的时间.

问题:通过在请求上调用"startAsynchronous"来启动请求时遇到了问题.委托回调永远不会被调用.它就像请求启动,下载其数据,但从不调用委托回调方法.

我的解决方案:当我同步启动请求时,一切似乎都运行正常(即回调等).这是正确的解决方案吗?

为什么同步调用有效,而不是异步?在Apples"TopSongs"示例之后,我正在为我的导入器类建模.

iphone asynchronous nsoperation asihttprequest nsoperationqueue

2
推荐指数
1
解决办法
3275
查看次数

是否有没有块使用NSOperationQueue的教程?

我的应用程序必须在iOS 3.2上运行,而诸如-addOperationWithBlock之类的方法只能在> 4.0中运行.

但是NSOperationQueue从iOS 2.0开始就可用,所以我想尝试一下"老方法".有没有人知道一个方便的教程,它显示了如何在没有块的情况下使用NSOperationQueue的基础知识?

performance multithreading nsoperation nsoperationqueue ios

2
推荐指数
1
解决办法
2619
查看次数

如何重复ASIHTTPRequest?

给出以下示例代码:

// ExampleModel.h

@interface ExampleModel : NSObject <ASIHTTPRequestDelegate> {

}

@property (nonatomic, retain) ASIFormDataRequest *request;
@property (nonatomic, copy) NSString *iVar;

- (void)sendRequest;


// ExampleModel.m

@implementation ExampleModel

@synthesize request;
@synthesize iVar;

# pragma mark NSObject

- (void)dealloc {
    [request clearDelegatesAndCancel];
    [request release];
    [iVar release];
    [super dealloc];
}

- (id)init {
    if ((self = [super init])) {
        // These parts of the request are always the same.
        NSURL *url = [[NSURL alloc] initWithString:@"https://example.com/"];
        request = [[ASIFormDataRequest alloc] initWithURL:url];
        [url release];
        request.delegate = …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c asihttprequest nsoperationqueue

2
推荐指数
1
解决办法
4303
查看次数

performSelectorOnMainThread导致"无法识别的选择器发送到实例"

首先感谢您抽出时间查看这个问题.

我正在尝试创建一个非常简单的UIViewController示例,我在一个线程中加载数据.线程被调用但是从那个线程,我无法回到主线程.下面的测试用例非常简单,我在XCode中创建了一个基于View的应用程序,并添加了NSOperationQueue代码.希望有人可以快速发现我的男生错误:)

代表很简单,.h是:

#import<UIKit/UIKit.h>

@class ViewbasedViewController;

@interface ViewbasedAppDelegate : NSObject <UIApplicationDelegate> {
 UIWindow *window;
 ViewbasedViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewbasedViewController *viewController;
@end
Run Code Online (Sandbox Code Playgroud)

而.m同样简单:

#import "ViewbasedAppDelegate.h"
#import "ViewbasedViewController.h"
@implementation ViewbasedAppDelegate
@synthesize window;
@synthesize viewController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        
 // Override point for customization after application launch.
 // Set the view controller as the window's root view controller and display.
 self.window.rootViewController = self.viewController;
 [self.window makeKeyAndVisible]; …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview nsoperationqueue

2
推荐指数
1
解决办法
2908
查看次数

创建多个NSURLConnections并使用sendAsynchronousRequest:queue:completionHandler:iOS 5方法

我在设置相对于sendAsynchronousRequest:queue:completionHandler:方法(NSURLConnection类)的正确配置方面遇到了一些困难.

我的方案如下:

我建立了一个管理不同NSURLConnections 的单例类.这个单例符号有一个NSOperation Queue(被叫downloadQueue),它向Web服务器发出请求并检索字符串路径(1).完成后,该路径用于下载Web服务器(2)中的文件.最后,当文件被正确下载后,我需要更新UI(3).

我只想出了第一个请求:我可以通过它下载路径.你能建议我一个方法来执行其他两个步骤吗?

这里几个问题:

  • 下载队列(downloadQueue)不是主要的,是否可以在该队列中打开新的NSURLConnection?换句话说,这是正确的吗?(参见代码片段中的注释)

  • 如果前一个问题是正确的,我如何获取主队列并更新UI?

这里我用来执行第一步的代码片段downloadQueue是一个可以通过访问器方法获得的实例变量(@property/ @synthesized);

// initializing the queue...
downloadQueue = [[NSOperation alloc] init];    

// other code here...
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[self downloadQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    if([data length] > 0 && error == nil) {

        // here the path (1)
        // how to perform a second connection?
        // what type of queue do I …
Run Code Online (Sandbox Code Playgroud)

networking nsurlconnection nsoperationqueue ios http-request

2
推荐指数
1
解决办法
5873
查看次数

取消NSOperation并立即将其从队列中删除

所以我已经考虑了几天了,我似乎无法找到一个好的方法来使它工作.

我有一个NSOperationQueue只允许一次一个操作.我从文档中了解到:

在OS X v10.6及更高版本中,取消操作会导致操作忽略它可能具有的任何依赖性.此行为使队列可以尽快执行操作的start方法.反过来,start方法将操作移动到完成状态,以便可以从队列中删除它.

当我发送取消时它将设置isCancelled为YES,但我的问题是:

假设我有20个NSOperation队列而且我刚刚取消了18号,它将一直保持NSOperationQueue到它可以运行并说它已经完成(我的NSOperation检查正确isCancelled)但是因为它保留在队列中它也保留在UITableView我设置dataSource的东西中喜欢myOperationQueue.operations.

这就是困扰我的用户,用户将点击Cancel将调用该cancel方法,NSOperation但操作仍会显示,因为它仍然在队列中.

我考虑过startcancel方法中调用,但不允许只有队列才可以start.

编辑:我也试着重写isFinishedcancel:

[self willChangeValueForKey:@"isFinished"];
_isFinished = YES;
[self didChangeValueForKey:@"isFinished"];
Run Code Online (Sandbox Code Playgroud)

它工作但它也发送startNSOperation队列中的下一个,它可以导致同时有2个NSOperation,我只想要一个.

objective-c uitableview nsoperation nsoperationqueue ios

2
推荐指数
1
解决办法
4516
查看次数

NSOperation userInfo字典

有没有办法如何userInfo NSDictionary获得一个NSOperation

基本上我想为NSOperation分配一个ID,以后我想检查一下,如果这个ID已经分配给了NSOperation

- (void)processSmthForID:(NSString *)someID {

    for (NSOperation * operation in self.precessQueue.operations) {

        if ([operation.userInfo[@"id"] isEqualToString:someID]) {
            // already doing this for this ID, no need to create another operation
            return;
        }

    }

    NSOperation * newOperation = ...
    newOperation.userInfo[@"id"] = someID;

    // enqueue and execute

}
Run Code Online (Sandbox Code Playgroud)

multithreading nsoperation nsoperationqueue ios

2
推荐指数
1
解决办法
777
查看次数

使NSOperations互斥

请参阅WWDC的视频https://developer.apple.com/videos/play/wwdc2015/226/ 发言人表明我们可以在两个相同类型的NSopeation实例之间添加依赖关系.示例显示警报的NS操作.通过实现这一点,我们可以确保我们不会同时抛出多个警报并使用户烦恼.如果已经显示一个警报,则下一个警报将等待.

我仍然无法弄清楚如何实现NSOperations交叉队列的这种依赖.更简单的话可以任何人展示以下两件事的例子(实现).

1.在队列1的操作A上添加操作B从队列2的依赖性的实现.

2.实现添加相同NSOperation类型的多个实例的依赖关系,即使它们位于不同的队列中.示例:如果我将"AlertOperation"的多个实例添加到不同的队列,我想确保它们之间仍然按顺序进行.

如果示例是在目标C中,我将不胜感激.如有需要,请要求进一步澄清.

nsoperation nsoperationqueue ios

2
推荐指数
1
解决办法
621
查看次数

如何在Swift 3中同时进行https请求

我有执行https请求的问题,如果请求没有任何错误,我从来没有得到消息,这是一个命令行工具应用程序,我有一个允许http请求的plist,我总是看到完成块.

typealias escHandler = ( URLResponse?, Data? ) -> Void

func getRequest(url : URL, _ handler : @escaping escHandler){    
let session = URLSession.shared
var request = URLRequest(url:url)
request.cachePolicy = .reloadIgnoringLocalCacheData
request.httpMethod = "GET"
let task = session.dataTask(with: url ){ (data,response,error) in
        handler(response,data)
}

task.resume()
}


func startOp(action : @escaping () -> Void) -> BlockOperation{

let exOp = BlockOperation(block: action)    
exOp.completionBlock = {

print("Finished")

}
return exOp
}

     for sUrl in textFile.components(separatedBy: "\n"){
     let url = URL(string: sUrl)!

        let queu = …
Run Code Online (Sandbox Code Playgroud)

concurrency http nsoperation nsoperationqueue swift3

2
推荐指数
1
解决办法
1021
查看次数