您的位置:首页 > 其它

局部对象变量调用分线程方法

2014-08-07 15:19 746 查看
demo1.

@implementation PEKViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
 

    PEKNewThread *newThread = [[PEKNewThread alloc] init];
    [newThread startLog];
    
}

@end


@implementation PEKNewThread

-(void)startLog
{
   [self performSelectorInBackground:@selector(log) withObject:nil];
//    [self log];<pre name="code" class="objc">typedef  void (^timeRreachBlock)(int times);

@interface PEKNewThread : NSObject
{
    int times;
}

-(void)startLog;
@property(nonatomic,copy)timeRreachBlock reachBlock;
@end


@implementation PEKNewThread

-(void)startLog
{
   [self performSelectorInBackground:@selector(log) withObject:nil];
//[self log];
}

-(void)log
{
    NSLog(@"I am log method");
    times ++;
    if (times == 3)
    {
        if (self.reachBlock)
        {
            [self reachBlock];
        }
    }
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(printLog) userInfo:nil repeats:YES];
}

-(void)printLog
{
    NSLog(@"log");
}

-(void)dealloc
{
    NSLog(@"I am newThead delloc");
}

@end


}-(void)log{ NSLog(@"I am log method"); [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(printLog) userInfo:nil repeats:YES];}-(void)printLog{ NSLog(@"log");}-(void)dealloc{ NSLog(@"I am newThead delloc");}@end


上述代码打印:


2014-08-07 14:24:44.453 ARCMemoryTest[17159:3507] I am log method
2014-08-07 14:24:44.454 ARCMemoryTest[17159:3507] I am newThead delloc
由上述demo可知,局部对象,调用分线程方法,方法一遍,对象就释放了。

demo2.
再来看一下,如果局部变量调用主线程的情况:
-(void)startLog
{
//   [self performSelectorInBackground:@selector(log) withObject:nil];
    [self log];
}

替换上述方法,打印结果如下:

2014-08-07 14:57:54.913 ARCMemoryTest[17240:60b] I am log method
2014-08-07 14:57:55.014 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.113 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.214 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.314 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.414 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.514 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.614 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.714 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.814 ARCMemoryTest[17240:60b] log
2014-08-07 14:57:55.914 ARCMemoryTest[17240:60b] log
demo3.
再来看一下如果局部变量分线程中有block块的情况:
设想:如果有对指向self的指针,调用方法的话,应该会crash,或者不调用该方法
验证:
@implementation PEKViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
 

    PEKNewThread *newThread = [[PEKNewThread alloc] init];
    [newThread startLog];
     newThread.reachBlock = ^(int times)
    {
        NSLog(@"I am block I am excute");
    };
    
}

@end

typedef  void (^timeRreachBlock)(int times);

@interface PEKNewThread : NSObject
{
    int times;
}

-(void)startLog;
@property(nonatomic,copy)timeRreachBlock reachBlock;
@end


#import "PEKNewThread.h"

@implementation PEKNewThread -(void)startLog { [self performSelectorInBackground:@selector(log) withObject:nil]; //[self log]; } -(void)log { NSLog(@"I am log method"); times ++; if (times == 3) { if (self.reachBlock) { [self reachBlock]; } } [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(printLog) userInfo:nil repeats:YES]; } -(void)printLog { NSLog(@"log"); } -(void)dealloc { NSLog(@"I am newThead delloc"); } @end


打印结果:

2014-08-07 15:12:54.943 ARCMemoryTest[17301:3507] I am log method
2014-08-07 15:12:54.944 ARCMemoryTest[17301:3507] I am newThead delloc
block没有打印,说明,block,如果局部对象调用异步方法的话,是需要被写成成员变量的(无论其中含有什么)
补充点常识:空指针调用方法不会奔溃,但是如果指针不为空,指向的内存释放了的话,就会奔溃,这也是为什么好多人都会习惯 self.property = nil,的原因
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐