【问题标题】:NSOperation userInfo DictionaryNSOperation userInfo 字典
【发布时间】:2013-04-27 10:56:11
【问题描述】:

有没有办法让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

}

【问题讨论】:

    标签: ios multithreading nsoperation nsoperationqueue


    【解决方案1】:

    NSOperation 意味着被子类化。只需设计您自己的子类。

    NSOperation 类是一个抽象类,用于封装与单个任务相关的代码和数据。因为它是抽象的,所以不要直接使用这个类,而是子类化或使用系统定义的子类之一(NSInvocationOperation 或 NSBlockOperation)来执行实际任务。

    read here

    我同意@Daij-Djan 关于userInfo 属性添加的观点。
    此属性可以作为NSOperation 的扩展来实现(请参阅他的实现答案)。
    但是,NSOperation 需要标识符是类的特化(您可以说新类是 IdentifiableOperation

    【讨论】:

    • 这是正确的。您可以简单地让您的子类发布一个自定义的 NSNotification 并将任何对象注册为观察者并保留对您子类的任何实例的引用。非常简单的 NSNotification 东西就可以了。
    • 由于 NSOperation 是一个 ABSTRACT 类,它应该被子类化以执行任何操作,你可以在那里实现任何你喜欢的。
    • 他没有要求通知 .. 当然这在没有任何东西的情况下是可行的,但是要添加一个不形成专业化的属性,一个不子类...类别和子类之间的区别。 -- 我同意在这里添加自定义的、专门的功能是非常错误的
    • 您明白引用文档的原因了吧? NSOperation 预计并打算被子类化。
    • 删除了我的反对票,但 uchugaka 应该阅读文档;)预期/预期? -- 他们只是和任何抽象类一样说你没有明确地使用它:D
    【解决方案2】:

    像这样在 NSOperation 上定义一个属性:

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    //category
    @interface NSOperation (UserInfo)
    @property(copy) NSDictionary *userInfo;
    @end
    
    static void * const kDDAssociatedStorageUserInfo = (void*)&kDDAssociatedStorageUserInfo;
    
    @implementation NSOperation (UserInfo)
    
    - (void)setUserInfo:(NSDictionary *)userInfo {
        objc_setAssociatedObject(self, kDDAssociatedStorageUserInfo, [userInfo copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSDictionary *)userInfo {
        return objc_getAssociatedObject(self, kDDAssociatedStorageUserInfo);
    }
    
    @end
    

    thaat 为您提供有关任何 NSOperation 或其子类的用户信息...例如NSBlockOperation 或 AFHTTPRequestOperation

    演示:

        //AFNetwork test
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.de"]]];
        operation.userInfo = @{@"url":operation.request.URL};
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"download of %@ completed. userinfo is %@", operation.request.URL, operation.userInfo);
            if(queue.operationCount==0)
                exit(1);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"download of %@ failed. userinfo is %@", operation.request.URL, operation.userInfo);
            if(queue.operationCount==0)
                exit(1);
        }];
        [queue addOperation:operation];
    

    【讨论】:

    • 不,它应该总是被子类化。这就是它的设计目的。
    • 您想为类添加功能。通用功能。你不想专门化它。这就是为什么我总体上同意并且我不会将特定的主要方法实现添加到基类中,但在这种情况下我不同意。
    • 类别用于水平扩展。这不是专业化,所以没有子类
    • 如上所述,NSOperation 是预期的并且打算被子类化。这就是它的设计。在其上添加类别是没有意义的,因为您应该始终将其子类化。类别会影响其他运行的子类(包括那些不是您编写但可能是您正在使用的 API 的一部分)
    • 根本没有这么说(它只谈到不使用它:))但我认为争论是没有意义的。 :)
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多