使用NSOperation使用,创建线程中传递多个参数
参考:http://blog.csdn.net/dqjyong/article/details/7677557
参考:http://stackoverflow.com/questions/2327617/calling-selector-with-two-arguments-on-nsthread-issue
使用NSInvocation对象
NSInvocation* deleteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(deleteDataAtPath:)]];
[deleteInvocation setTarget:self];
[deleteInvocation setSelector:@selector(deleteDataAtPath:)];//给NSInvocation对象添加对应的动作
// // self, _cmd, ... 参数索引必须是2以后
[deleteInvocation setArgument:&cachePath atIndex:];
//用NSInvocation对象来初始化一个NSOperation的子类NSInvocationOperation对象
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction]; //初始化一个操作队列
NSOperationQueue* operationQueue=[[NSOperationQueue alloc] init]; //在操作队列中加入操作
[operationQueue addOperation:operation];
[operation release];
具体实现:
NSThread+ManyObjects.h
:
@interface NSThread (ManyObjects) + (void)detachNewThreadSelector:(SEL)aSelector
toTarget:(id)aTarget
withObject:(id)anArgument
andObject:(id)anotherArgument; @end
NSThread+ManyObjects.m:
@implementation NSThread (ManyObjects) + (void)detachNewThreadSelector:(SEL)aSelector
toTarget:(id)aTarget
withObject:(id)anArgument
andObject:(id)anotherArgument
{
NSMethodSignature *signature = [aTarget methodSignatureForSelector:aSelector];
if (!signature) return; NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:aTarget];
[invocation setSelector:aSelector];
[invocation setArgument:&anArgument atIndex:];
[invocation setArgument:&anotherArgument atIndex:];
[invocation retainArguments]; [self detachNewThreadSelector:@selector(invoke) toTarget:invocation withObject:nil];
} @end
调用:
[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" andObject:@"somepassword"];
另一种实现:
参考:http://blog.engledew.com/post/730804661/nsthread-a-selector-with-multiple-arguments
#import <Foundation/Foundation.h> @interface SEThreadInvocation : NSObject
{
NSThread * _thread;
NSInvocation * _invocation;
} @property(nonatomic,retain) NSThread * thread;
@property(nonatomic,retain) NSInvocation * invocation; - (id) initWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments;
+ (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObjects:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
- (void) start; @end
#import "SEThreadInvocation.h" @implementation SEThreadInvocation @synthesize thread = _thread;
@synthesize invocation = _invocation; - (id) initWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments
{
if (self = [super init])
{
_thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
_thread.name = @"SEThreadInvocation"; NSMethodSignature * signature = [target methodSignatureForSelector:selector]; self.invocation = [NSInvocation invocationWithMethodSignature:signature]; [_invocation setTarget:target];
[_invocation setSelector:selector]; NSInteger i = ; // self, _cmd, ... for (id argument in arguments)
{
[_invocation setArgument:&argument atIndex:i++];
} [_invocation retainArguments];
}
return self;
} - (void)start
{
[_thread start];
} - (void)run
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [_invocation invoke]; [pool release];
} + (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObjects:(id)firstObject, ...
{
NSMutableArray * arguments = [NSMutableArray array]; if (firstObject)
{
[arguments addObject:firstObject]; id argument;
va_list objectList;
va_start(objectList, firstObject); while (argument = va_arg(objectList, id))
{
[arguments addObject:argument];
} va_end(objectList);
} SEThreadInvocation * threadInvocation = [[SEThreadInvocation alloc] initWithTarget:target selector:selector arguments:arguments];
[threadInvocation start];
[threadInvocation autorelease];
} - (void) dealloc
{
[_invocation release];
[_thread release]; [super dealloc];
} @end
[SEThreadInvocation detachNewThreadSelector:@selector(request:processData:)
toTarget:self
withObjects:request, data, nil];
使用NSOperation使用,创建线程中传递多个参数的更多相关文章
- c语言线程中传输多个参数
前言:c语言中创建一条线程,但是需要传送多个参数给线程的话我们自然会想到通过传送数组或者结构体来实现,下面我们来看看如何在创建线程的时候传送结构体和数组. #include <stdio.h&g ...
- MyBatis 中传递多个参数的 4 种方式
方式 1 :封装成对象入参 #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...
- Mybatis中传递多个参数的方法总结
一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...
- Mybatis接口中传递多个参数
1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...
- C# 中传递多个参数给多线程
1.方式一:使用ParameterizedThreadStart委托 如果使用了ParameterizedThreadStart委托,线程的入口必须有一个object类型的参数,且返回类型为void. ...
- struts2 action中传递两个参数到url
<action name="outInDetail" class="formManage_outInDetailAction"> <resul ...
- vue v-show与v-for同时配合v-bind使用并在href中传递多个参数的使用方法
最近在项目中,因为还没使用前端构建工具,还在使用vue+jquery方法渲染页面 碰到几个小问题,在此记录下作为vue学习之路上的一个小知识点 需求:1.数据列表存在与否状态,没有数据显示默认提示,有 ...
- A标签中传递的中文参数到Servlet 后台request.getParameter()接收时出现中文乱码
package util; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequ ...
- CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别(1)
这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...
随机推荐
- 图像质量评价指标之Matlab实现
在图像处理算法研究中,很多时候需要有客观评价指标来对算法的性能进行评价. 比如,在图像复原.图像滤波算法研究中,需要采用客观评价指标来定量的来测试算法恢复出的图像相对于参考图像的好坏程度. 本文介绍文 ...
- C++ Primer : 第九章 : 顺序容器的定义、迭代器以及赋值与swap
顺序容器属于C++ STL的一部分,也是非常重要的一部分. 顺序容器包括: std::vector,包含在头文件<vector>中 std::string, 包含在头文件<strin ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
- react 不能往组件中传入属性的值为 undefined
在使用 andt design 的时候遇到个需求,需要清除 Select 组件选中后的值,让它变成什么都没选中,显示 placeholder 刚开始以为设置为 null 即可,结果发现设置为 null ...
- Android度量单位说明(DIP,DP,PX,SP)
本文转载于:http://blog.sina.com.cn/s/blog_6b26569e0100xw6d.html (一)概念 dip: device independent pixels(设备独立 ...
- 异步IO/数据库/队列/缓存
同步与异步的性能区别 mport gevent def task(pid): """ Some non-deterministic task ""& ...
- 论文笔记之:Visual Tracking with Fully Convolutional Networks
论文笔记之:Visual Tracking with Fully Convolutional Networks ICCV 2015 CUHK 本文利用 FCN 来做跟踪问题,但开篇就提到并非将其看做 ...
- dump java
http://www.gamlor.info/wordpress/2011/09/visualvm/ https://visualvm.java.net/zh_CN/gettingstarted.ht ...
- jquery选取iframe
$(window).load(function(){ $('iframe').contents().find('form'); })
- HTML和JSON的数据交互-HTML模板
直接上源码,原文http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/ <!DOCTYPE html ...