标签: nsthread

如何检查Swift 3中的当前线程?

如何检查哪一个是Swift 3中的当前线程?

在Swift的早期版本中,可以通过执行以下操作来检查当前线程是否是主要线程:

NSThread.isMainThread()
Run Code Online (Sandbox Code Playgroud)

multithreading nsthread ios swift3

89
推荐指数
7
解决办法
3万
查看次数

目标C找到方法的调用者

有没有办法确定某个特定的代码行method

debugging objective-c nsthread ios

88
推荐指数
6
解决办法
3万
查看次数

在主线程和后台线程中调度NSTimer的区别?

当我调用scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:主线程并将时间间隔设置为5秒时,下面的代码被执行,并且在5秒后调用定时器选择器.

但是如果我在某个后台线程中尝试相同,下面的代码scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:将不会被执行,它将等待定时器触发然后执行.当然,为了在后台线程中运行计时器,我首先得到了一个实例NSRunLoop并运行它.

有没有办法在后台线程中设置定时器并使其无阻塞,所以代码在它立即执行后?

nstimer nsthread ios5

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

MBProgressHUD阻止用户交互

我的应用程序MBProgressHUD在屏幕上有一个CLLocationManager用户当前位置在后台的一个单独的线程.有时位置过程开始花费这么长时间,显然我想让用户决定离开或不离开屏幕.问题是用户界面似乎被阻止,MBProgressHUD因此用户无法按下后退按钮.

有没有任何实现设计来解决这个问题?

objective-c nsthread cllocationmanager mbprogresshud

33
推荐指数
1
解决办法
8808
查看次数

NSThread vs. NSOperationQueue vs. ??? 在iPhone上

目前我正在使用NSThread另一个线程缓存图像.

[NSThread detachNewThreadSelector:@selector(cacheImage:) toTarget:self withObject:image];
Run Code Online (Sandbox Code Playgroud)

交替:

[self performSelectorInBackground:@selector(cacheImage:) withObject:image];
Run Code Online (Sandbox Code Playgroud)

或者,我可以使用 NSOperationQueue

NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(cacheImage:) object:image];
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:invOperation];
Run Code Online (Sandbox Code Playgroud)

有没有理由改变NSThread?GCD是iPhone上发布的第四个选项,但除非有显着的性能提升,否则我宁愿坚持使用适用于大多数平台的方法.


根据@ Jon-Eric的建议,我选择了NSOperationQueue/ NSOperation子类解决方案.它工作得很好.该NSOperation班是足够灵活,你可以调用,块或定制子类使用它,这取决于你的需求.无论您如何创建自己,NSOperation都可以在准备运行时将其放入操作队列中.如果需要,这些操作可以作为您放入队列的对象,也可以作为独立的异步方法运行.由于您可以轻松地同步运行自定义操作方法,因此测试非常简单.

我在一些项目中使用了相同的技术,因为我问了这个问题,我对它保持我的代码和我的测试干净,有条理和快乐异步的方式感到高兴.

A +++++++++++++++++++++++++

iphone nsthread nsoperationqueue grand-central-dispatch

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

NSOperation - 强制操作动态等待其他操作

我正在尝试实现一个操作队列,我有以下场景:

NSOperation A
NSOperation B
NSOperation C
NSOperation D
NSOperationQueue queue
Run Code Online (Sandbox Code Playgroud)

我开始加入Aqueue.

在执行期间,A我需要从中获取一些数据B,我无法继续,A直到B返回我需要的内容.

B取决于CC取决于相同的情况将发生D.

为了管理这个,每个NSOperation我都有这个代码:

NSOperation *operation; //This can be A, B, C, D or any other NSOperation

[self setQueuePriority:NSOperationQueuePriorityVeryLow]; //Set the current NSOperation with low priority

[queue addOperation: operation]; //Add the operation that I want to the queue

while(!operation.isFinished && !self.isCancelled){} //I need to wait the operation that …
Run Code Online (Sandbox Code Playgroud)

objective-c nsoperation nsthread nsoperationqueue ios

27
推荐指数
2
解决办法
2万
查看次数

帮助iOS上的多线程?

我有一个利用OpenEars和Flite库的应用程序.问题是Flite库是资源密集型的,它冻结了我的应用程序.我怀疑在后台线程上运行Flite会解决问题,但我知道该怎么做.

也就是说,如何在iOS中实现后台线程

如果有人能指出我的一些教程,分享一些示例代码或任何可以帮助我解决这个问题的一般建议,我将不胜感激.

iphone multithreading nsthread ios

25
推荐指数
2
解决办法
4万
查看次数

主线程中的di​​spatch_get_main_queue()

我有一些方法可以在某些情况下改变UI.

例如:

-(void) myMethod {

  if(someExpressionIsTrue) {
    // make some UI changes
    // ...
    // show actionSheet for example
  }
}
Run Code Online (Sandbox Code Playgroud)

有时从某些人那里myMethod调用.mainThreadother thread

这就是为什么我希望这些UI更改可以肯定地执行mainThread.

我改变了myMethod这种方式所需的部分:

if(someExpressionIsTrue) {
     dispatch_async(dispatch_get_main_queue(), ^{
        // make some UI changes
        // ...
        // show actionSheet for example
      });
}
Run Code Online (Sandbox Code Playgroud)

所以问题:

  • dispatch_async(dispatch_get_main_queue() 在主线程中调用是否安全且良好的解决方案?它会影响性能吗?
  • 这个问题可以用另一种更好的方式解决吗?我知道我可以检查它是否是一个使用[NSThread isMainThread]方法的主线程,并且dispatch_async仅在其他线程的情况下调用,但它将使我再创建一个方法或阻止这些UI更新.

multithreading objective-c nsthread grand-central-dispatch

25
推荐指数
2
解决办法
4万
查看次数

保持NSThread存活并在其上运行NSRunLoop

所以我正在开始一个新的NSThread,我希望以后可以通过调用来使用它performSelector:onThread:....根据我的理解,调用方法将该调用添加到该线程上的runloop,因此在下一次迭代时,它将弹出所有这些调用,然后调用它们,直到没有任何东西可以调用.所以我需要这种功能,一个空闲的线程准备工作,我可以调用它.我当前的代码如下所示:

   - (void)doInitialize
   {
       mThread =  [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
       [mthread start];
   }

   - (void)runThread
   {
       NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

       // From what I understand from the Google machine is that this call should start the
       // runloop on this thread, but it DOESN'T. The thread dies and is un-callable
       [[NSRunLoop currentRunLoop] run];

       [pool drain];
   }

   - (void)scheduleSomethingOnThread
   {
       [self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
   }
Run Code Online (Sandbox Code Playgroud)

但是线程没有保持活动状态,performSelector:onThread没有做任何事情.我该怎么做正确的方法?

cocoa objective-c foundation nsthread nsrunloop

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

哪个是GCD,NSThread或NSOperationQueue中最好的?

什么是iOS中多线程的最佳方式,因为我们有三个选项GCD NSThread,和NSOperationQueue?我很困惑哪一个是最好的?如果没有,那么哪个应该用于什么情况以及它们如何不同,如果有人有一些很好的使用示例NSOperationQueue,请分享以便我可以学习.

multithreading nsthread nsoperationqueue grand-central-dispatch ios

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