nsthread

po [NSThread currentThread]

点点圈 提交于 2019-12-06 09:19:31
问题 When I do po [NSThread currentThread], I got {name = (null), num = 4} When I look to the left I see: Looks like it's Thread number 6, not 4. Also what properties do we need to call to get that thread numbers anyway? [NSThread currentThread].number? Doesn't exist though. 回答1: Thread numbers are meaningless, pretty much. The thread instance, though, is a singleton per thread. You could use the NSThread's address, by coincidence. Better, still, would be to dip down to the mach_* API and grab the

Any way to make dispatch_queue_t work in single thread?

时间秒杀一切 提交于 2019-12-06 06:57:14
问题 Here is my code: @interface MyObject () @property(nonatomic) dispatch_queue_t queue; @end @implementation MyObject { NSThread *_check; } - (id)init { self = [super init]; if (self) { _queue = dispatch_queue_create("com.Thread.queue", NULL); dispatch_async(_queue, ^{ _check = [NSThread currentThread]; //for ex. thread number = 3 //some code here... }); } return self; } - (void)someMethod:(MyObjClass *)obj { dispatch_async(_queue, ^{ //need th if (_check != [NSThread currentThread]) { // it is

Detect end of performSelectorInBackground:withObject:

三世轮回 提交于 2019-12-06 05:45:00
问题 I have an asynchronous server request in my iOS app: [self performSelectorInBackground:@selector(doSomething) withObject:nil]; How can I detect the end of this operation? 回答1: Put a call at the end of the doSomething method?! - (void)doSomething { // Thread starts here // Do something // Thread ends here [self performSelectorOnMainThread:@selector(doSomethingDone) withObject:nil waitUntilDone:NO]; } 回答2: If you just want to know when it's finished (and don't want to pass much data back - at

How to make NSRunLoop work inside a separate thread?

亡梦爱人 提交于 2019-12-06 05:19:17
问题 Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@"BEFORE"); [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20]]; NSLog(@"AFTER"); } @end int main(int argc, char *argv[]) { myObject *object = [[myObject alloc] init]; [NSThread detachNewThreadSelector:@selector(function:) toTarget:object withObject:nil]; @autoreleasepool { return UIApplicationMain(argc, argv, nil,

OS X Cocoa timer not fired when application on background

坚强是说给别人听的谎言 提交于 2019-12-06 02:32:52
A third-party library provides a function I need to call every 100ms. Setting up a timer to do that works very well as long as my app is on foreground. When my app is on background timer works for a while but after a about a minute timer is called only after 10 second delay. The same happened when I created a separate thread with usleep-function. Is there any way I can keep timer running while my app is on background? Use beginActivityWithOptions:reason: to disable app nap for your application. You should try and re-architect to avoid these sorts of frequent timers, especially when your

Running NSTimer on a thread

谁说我不能喝 提交于 2019-12-06 00:46:12
I am trying to run a NSTimer on a thread using iPhone SDK 3.0. I think I am doing everything correctly (new runloop etc.). If I call [timer invalidate] on viewDidDissappear though I get this error: bool _WebTryThreadLock(bool), 0x3986d60: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... Program received signal: “EXC_BAD_ACCESS”. Here is my code: - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [activityIndicator startAnimating]; NSThread* timerThread =

Execute function every X minutes in background doesn't work

别等时光非礼了梦想. 提交于 2019-12-05 21:17:31
I use this code to execute function every X minutes: - (void)executeEveryOneMinute { [self myFunction]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self executeEveryOneMinute]; }); } And it works when app is in foreground. But when app goes background it doesn't work anymore. When I return app to foreground again it execute function once. And continue to call function every minute again. So how to make this to work in background too? See the Background Execution and Multitasking section of the iOS App Programming Guide: App

Difference between Sleep and NSRunLoop runMode:beforeDate:

我的梦境 提交于 2019-12-05 16:55:17
I have recently found that when waiting for my NSURLConnections to come through it works much better if I tell the waiting thread to do: [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; instead of [NSThread sleepForTimeInterval:1]; After reading a bit about NSRunLoop runMode:beforeDate: it sounds like it is preferable over sleep just about always. Have people found this to be true? Yes, NSRunLoop is better because it allows the runloop to respond to events while you wait. If you just sleep your thread your app will block even if events arrive (like

iOS开发 多线程(二) NSThread的使用(转)

你说的曾经没有我的故事 提交于 2019-12-05 14:31:50
每个iOS应用程序都有个专门用来更新显示UI界面、处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验。一般的解决方案就是将那些耗时的操作放到另外一个线程中去执行,多线程编程是防止主线程堵塞,增加运行效率的最佳方法 iOS支持多个层次的多线程编程,层次越高的抽象程度越高,使用也越方便,也是苹果最推荐使用的方法。下面根据抽象层次从低到高依次列出iOS所支持的多线程编程方法: 1. Thread :是三种方法里面相对轻量级的,但需要管理线程的生命周期、同步、加锁问题,这会导致一定的性能开销 2. Cocoa Operations :是基于OC实现的,NSOperation以面向对象的方式封装了需要执行的操作,不必关心线程管理、同步等问题。NSOperation是一个抽象基类,iOS提供了两种默认实现:NSInvocationOperation和NSBlockOperation,当然也可以自定义NSOperation 3. Grand Central Dispatch (简称GCD,iOS4才开始支持):提供了一些新特性、运行库来支持多核并行编程,它的关注点更高:如何在多个cpu上提升效率 这篇文章简单介绍了第一种多线程编程的方式,主要是利用NSThread这个类,一个NSThread实例代表着一条线程 一

iOS多线程编程之一——NSThread线程管理

不打扰是莪最后的温柔 提交于 2019-12-05 14:31:36
iOS多线程编程之一——NSThread线程管理 NSTread是iOS中进行多线程开发的一个类,其结构逻辑清晰,使用十分方便,但其封装度和性能不高,线程周期,加锁等需要手动处理。 一、NSThread类方法总结 获取当前线程 + (NSThread *)currentThread; 这个方法通过开启一个新的线程执行选择器方法 + (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument; 线程用法示例如下: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [NSThread detachNewThreadSelector:@selector(log) toTarget:self withObject:nil]; for (int i=0; i<100; i++) { NSLog(@"%@=%d",[NSThread currentThread],i); } } -(void)log{ for (int i=0; i<100; i++) { NSLog(@"%@=%d",