标签: nsthread

在线程上运行NSTimer

我正在尝试使用iPhone SDK 3.0在线程上运行NSTimer.我想我正在做的一切正确(新的runloop等).如果我在viewDidDissappear上调用[timer invalidate]虽然我收到此错误:

bool _WebTryThreadLock(bool),0x3986d60:试图从主线程或Web线程以外的线程获取Web锁.这可能是从辅助线程调用UIKit的结果.现在崩溃...程序收到信号:"EXC_BAD_ACCESS".

这是我的代码:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [activityIndicator startAnimating];
    NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(timerStart) object:nil]; //Create a new thread
    [timerThread start]; //start the thread
}

-(void)timerStart
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    //Fire timer every second to updated countdown and date/time
    timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];
    [runLoop run];
    [pool release];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [timer invalidate];
}
Run Code Online (Sandbox Code Playgroud)

当我删除使计时器无效的行时,一切正常.我不应该使它无效或我是否犯了其他错误?

谢谢

iphone nstimer nsthread runloop

3
推荐指数
1
解决办法
9459
查看次数

iPhone - 帮助MBProgressHUD,Sudzc,Web服务和NSThread

我有以下代码,它连接到Web服务并返回一个Categories数组.

我正在使用以下SOAP Web服务包装器:

http://www.sudzc.com/

..以及MBProgressHUD活动指标的以下内容:

https://github.com/jdg/MBProgressHUD

我想有一个HUD进度指示器表明它正在连接和抓取结果.我目前正在使用MBProgressHUD以达到预期的效果,但我注意到它不能正常工作.在tableView加载和显示我的实际单元格之前,进度指示器消失.我也在MBProgressHUD我的应用程序的不同区域使用,但通常每次我连接到Web服务以获取结果.

是否有人能够指导我如何解决以下代码才能正常工作?我认为这可能与在显示进度指示器后触发回调方法有关.不太确定如何MBProgressHUD使用回调方法正确实现.

这是我"试图"让它发挥作用的可怕尝试.

- (void)showHUD {
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

    // Add HUD to screen.
    [self.navigationController.view addSubview:HUD];

    // Register for HUD callbacks so we can remove it from the window at the right time.
    HUD.delegate = self;

    HUD.labelText = @"Loading";

    // Show the HUD while the provided method executes in a new thread
    [HUD showWhileExecuting:@selector(runLocalNotificationHandler) onTarget:self withObject:nil animated:YES];
}

- (void)runLocalNotificationHandler {
    NSAutoreleasePool …
Run Code Online (Sandbox Code Playgroud)

iphone web-services objective-c nsthread uiactivityindicatorview

3
推荐指数
1
解决办法
5725
查看次数

在dispatch_queue_t上检查是否相等

如何检查dispatch_queue_t变量之间的相等性?

dispatch_queue_t currentQueue = dispatch_get_current_queue();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
if (currentQueue == mainQueue) {

}
Run Code Online (Sandbox Code Playgroud)

来自文档:

typedef struct dispatch_queue_s *dispatch_queue_t;
Run Code Online (Sandbox Code Playgroud)

我不确定,但这是否意味着它是一个指向dispatch_queue_s结构的指针?

由于我无法检查指针的相等性,我不知道如何检查dispatch_queue_t是否与另一个相同?

cocoa cocoa-touch nsthread grand-central-dispatch

3
推荐指数
1
解决办法
5093
查看次数

没有runloop的线程

我正在阅读核心动画编程指南,在"交易"一章中,我看到了这一点

要点:从没有runloop的线程修改图层属性时,必须使用显式事务.

但是来自Apple的NSRunLoop文档

您的应用程序无法创建或显式管理NSRunLoop对象.每个NSThread对象(包括应用程序的主线程)都会根据需要自动为其创建NSRunLoop对象.

这不是说每个线程都有一个runloop吗?或者不是由NSThread创建的线程,例如POSIX的pthread

nsthread runloop ios

3
推荐指数
1
解决办法
1957
查看次数

在iOS中的不同线程上执行后台任务

如何在不同线程的后台执行某些操作,如果它在主线程上执行则会阻止我的应用程序的UI.任何人都知道该怎么做?

即使它在后台打印NSLog也没关系.即使用户按下HOME按钮,我也希望运行以下内容.在我的viewController中我这样做了:

- (IBAction)btnStartClicked:(UIButton *)sender {
    [NSThread detachNewThreadSelector:@selector(StartBGTask) toTarget:self withObject:nil];
     }

-(void)StartBGTask{
    [[[UIApplication sharedApplication] delegate] performSelector:@selector(startThread)];  
  }
Run Code Online (Sandbox Code Playgroud)

并在appDelegate.mi中有这个方法

 -(void) startThread {
 @autoreleasepool {
    for (int i = 0; i < 100; i++) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"current progress %d", i);
        });
        [NSThread sleepForTimeInterval:1];
    }
   }
 }
Run Code Online (Sandbox Code Playgroud)

它以1秒的间隔打印1到100之间的整数.

background-process nsthread ios

3
推荐指数
2
解决办法
6800
查看次数

OS X Cocoa计时器在后台应用程序时不会触发

第三方库提供了每100ms调用一次的功能.只要我的应用程序位于前台,设置计时器就能很好地工作.当我的应用程序在后台计时器工作一段时间但是在大约一分钟计时器仅在10秒延迟后调用.当我使用usleep-function创建一个单独的线程时也发生了同样的事情.当我的应用程序在后台时,有什么办法可以让计时器继续运行吗?

macos sleep nstimer nsthread

3
推荐指数
1
解决办法
1696
查看次数

在后台每 X 分钟执行一次函数不起作用

我使用此代码每 X 分钟执行一次函数:

- (void)executeEveryOneMinute
{
    [self myFunction];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self executeEveryOneMinute];

    });
}
Run Code Online (Sandbox Code Playgroud)

它在应用程序处于前台时工作。
但是当应用程序进入后台时,它不再起作用。
当我再次将应用程序返回到前台时,它会执行一次功能。
并继续每分钟再次调用函数。

那么如何让它在后台工作呢?

iphone objective-c nsthread ios ios7

3
推荐指数
1
解决办法
3570
查看次数

dispatch_queue_t是串行队列,为什么它甚至存在于多任务概念中?

我是iPhone开发的新手,并且通过GCD概念进行多线程处理.

'dispatch_queue_t'创建了一个串行队列,我已经读过一个串行队列一次只能执行一个作业.GCD旨在同时执行多个任务,然后为什么串行队列存在?

例如,我想做2个任务.任务A和任务B.我创建一个串行队列来执行这两个任务.我在主队列中这样做.这是我正在做的代码:

dispatch_queue_t my_serial_queue = dispatch_queue_create("queue_identifier", 0);

    dispatch_sync(my_serial_queue, ^{
        NSLog(@"Task 1");
    });

    dispatch_sync(my_serial_queue, ^{
        NSLog(@"Task 2");
    });
Run Code Online (Sandbox Code Playgroud)

现在,根据规则,任务将串行执行,因为它是串行队列,即首先执行任务A,然后在任务A完成后,将执行任务B. 而且它在日志中给我相同的输出.

所以,我的问题是,如果我想同时执行这两项任务怎么办?如果这个问题的答案是为任务B创建另一个串行队列,那么代码的结构应该是这样的:

dispatch_queue_t my_serial_queue_1 = dispatch_queue_create("queue_identifier_1", 0);
dispatch_queue_t my_serial_queue_2 = dispatch_queue_create("queue_identifier_2", 0);


    dispatch_sync(my_serial_queue_1, ^{
        NSLog(@"Task 1");
    });

    dispatch_sync(my_serial_queue_2, ^{
        NSLog(@"Task 2");
    });
Run Code Online (Sandbox Code Playgroud)

我得到了相同的输出.原因是我使用'dispatch_sync'调用而不是'dispatch_async'调用.但是当我在不同的队列中运行这两个任务时,它们不应该同时执行吗?如果没有,那么我们为什么要创建一个不同的队列呢?我可能通过'dispatch_async'调用使用相同的队列来同时执行这两个任务.

我真的需要回答这个问题,因为在将来设计我的多任务应用程序的结构之前,它会更好地指导我.

multithreading multitasking nsthread grand-central-dispatch ios

3
推荐指数
1
解决办法
3380
查看次数

pthread vs NSThread:速度更快

在Cocoa中,NSThread是否比pthread更快?是否有任何性能提升?是忽略不计的吗?

c++ cocoa multithreading pthreads nsthread

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

NSTimer不在子线程中触发

我试图NSTimer在子线程中触发.我的代码基本上是这样的:

-(void) handleTimer:(NSTimer *)theTimer {  
    NSLog(@"timer fired");  
}

-(void) startMyThread {  
    // If I put timer in here, right before I start my new thread, it fires.  
    // [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:NO];  
    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];  
}

// This method is called from inside the new thread  
-(void) playNote:(Note *)theNote withTemplate:(NSString *)theTemplate X:(int)x andY:(int)y {
    NSLog(@"before timer");  
    // The timer doesn't fire in here. (But the print statements do.)  
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:NO];
    NSLog(@"after …
Run Code Online (Sandbox Code Playgroud)

objective-c nstimer nsthread ios

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