使用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 ...
随机推荐
- Sprint第二个冲刺(第五天)
一.Sprint 计划会议: 容杰龙继续完善昨天的SQLite修改数据操作,待全部操作完善后交给炜杰进行布局规范和整合. 二.Sprint周期: 看板: 燃尽图:
- 刚查了,Z3795不支持EPT,即WP8开发必须的SLAT,看来只能作为简单的WINDOWS备机了
刚查了,Z3795不支持EPT,即WP8开发必须的SLAT,看来只能作为简单的WINDOWS备机了,也就只能做做文档编辑,脚本编写之类的. 数据来源 http://ark.intel.com/zh-C ...
- SQL注入测试平台 SQLol -1. 简介与安装
最近下载了SQLol测试了一下,感觉挺好玩的,做一下记录. SQLol是一个可配置得SQL注入测试平台,它包含了一系列的挑战任务,让你在挑战中测试和学习SQL注入语句,SQLol还是比较有创意的项目. ...
- URAL 1160 Network(最小生成树)
Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...
- php文件删除
<?php $dirname="shangchuan/uploads"; deldir($dirname); function deldir($dirname){ if(fi ...
- [PA2014] [BZOJ 3709]~[BZOJ 3719] 合集
今天起尝试做套题喵~ (当然是因为被最大流的题目弄得恶心死了) 一共是 10 道题一道一道做 预计 3~4 内做完 尽情期待 [BZOJ 3709]Bohater 一眼就能感受到贪心的气息 因为很直观 ...
- bufferedReader 乱码问题
public static void main(String arsg[]) throws Exception{ BufferedReader bufferedReader = new Buffere ...
- javase tutorial
http://docs.oracle.com/javase/tutorial/index.html
- 初识boost之boost::share_ptr用法
boost中提供了几种智能指针方法:scoped_ptr shared_ptr intrusive_ptr weak_ptr,而标准库中提供的智能指针为auto_ptr. 这其中,我最喜欢,使用最多的 ...
- kaptcha随机验证码的使用详解,超实用
效果图: 官方地址:https://code.google.com/p/kaptcha/w/list 1.把下载的kaptcha-2.3.2.jar添加到lib中 2.配置web.xml增加servl ...