nsthread

NSThread number on iOS? [duplicate]

落花浮王杯 提交于 2019-11-29 09:58:44
This question already has an answer here: po [NSThread currentThread] 2 answers When you print a NSThread's description, you get something like this: <NSThread: 0x1e511b70>{name = (null), num = 1} Is this "num" available somehow? This is for debugging only, so it does not need to clear Apple's approval process. That number is actually an ivar in NSThread's private implementation class. The class is _NSThreadInternal , and its name is "_private". Inside that object, the ivar is seqNum . You can pull it directly if you're willing to rely on undocumented key paths. This'll do it (and good call

checking for equality on dispatch_queue_t

百般思念 提交于 2019-11-29 08:23:40
How can I check for equality between dispatch_queue_t vars? dispatch_queue_t currentQueue = dispatch_get_current_queue(); dispatch_queue_t mainQueue = dispatch_get_main_queue(); if (currentQueue == mainQueue) { } from the docs: typedef struct dispatch_queue_s *dispatch_queue_t; I'm not sure but does this mean that it's a pointer to a dispatch_queue_s struct? Since I can't check equality on pointers, I'm not sure how can I check if a dispatch_queue_t is the same as another? This depends on the queue you're on. In this particular case use: if ([NSThread isMainThread]) {} In general, you can use

How to exit NSThread

一笑奈何 提交于 2019-11-29 08:13:30
I am using a thread like this, [NSThread detachNewThreadSelector:@selector(myfunction) toTarget:self withObject the thread is running correctly, I want to quit the thread in the middle,how can I do this.If I use [NSThread exit] the application is hanging. In which thread are you running "[NSThread exit]"? [NSThread exit] runs in the current thread so you need to call this as part of the myfunction selector. If you call it in the main thread, it will just exit the main thread. Also, it's not a good idea to stop threads like this as it prevents the thread being exited from cleaning up resources.

Does every thread need its own autorelease pool?

北战南征 提交于 2019-11-29 05:06:46
Does every thread have to have its own pool? I am writing an iPhone app which uses threads. If I do not put a pool on a thread it complains abut leaking. What I wanted to do was to store some object which outlives the thread. How can I do it? No, every NSThread has its own NSRunLoop , but not its own NSAutoreleasePool . Thus you have to create one and if you are performing a large operation or a operation that needs a lot of time, you really should drain the pool from time to time to keep your memory footprint low. Object storage isn't bound to a thread at all, you can access every object from

is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread)

一笑奈何 提交于 2019-11-28 19:41:12
Is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread). That is, there are are there any gottcha's for this purpose? Background want to call back to main UI thread from a background thread (e.g. performSelectorInBackground) could use performSelectorOnMainThread to communicate back, but wondering if it is OK to use a notification? For example [[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object:self]; Actually there is a gottcha; you'll crash randomly! That has been my experience. This has to do

Order of operations in runloop on iOS

落花浮王杯 提交于 2019-11-28 19:00:26
What is the order of operations on iOS? I'm thinking sepcifically about timing of setNeedsLayout and layoutSubviews setNeedsDisplay and drawRect touch recognition [NSTimer scheduledTimerWithTimeInterval:0.000001 tar(...)] dispatch_async(dispatch_get_main_queue(), ^{ /* code */} As an example of an answer I would like to receive it could be in this format: dispatch_async on main Happens before the next runcycle drawRect Happens at the end of the runcycle rob mayoff (Parts of this are copied from my answer to a similar question .) It turns out that the run loop is complicated, and a simple

Keep NSThread alive and run NSRunLoop on it

瘦欲@ 提交于 2019-11-28 18:45:57
So I'm starting a new NSThread that I want to be able to use later by calling performSelector:onThread:... . From how I understand it calling that methods add that call to the runloop on that thread, so on its next iteration it will pop all these calls and subsequently call them until there is nothing left to call. So I need this kind of functionality, an idle thread ready for work that I just can call upon it. My current code looks like this: - (void)doInitialize { mThread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil]; [mthread start]; } - (void)runThread {

Help with multi-threading on iOS?

拥有回忆 提交于 2019-11-28 17:53:38
I have an application which utilizes OpenEars and the Flite library. The problem is that the Flite library is resource intensive and it's freezing up my app. I suspect that running Flite on a background thread will fix things, but I have no idea how to do so. That said, how do I implement a background thread in iOS ? I'd appreciate if anyone can point me to some tutorials, share some sample code, or any general advice that would help me solve this problem. The Concurrency Programming Guide by Apple is a nice reading. Concurrent programming is not something you might want to pick up by copying

Grand Central Dispatch vs. NSThread

安稳与你 提交于 2019-11-28 17:17:46
I created some test code for NSThread and Grand Central Dispatch (GCD): - (void)doIt:(NSNumber *)i { sleep(1); NSLog(@"Thread#%i", [i intValue]); } - (IBAction)doWork:(id)sender { for (int i = 0; 10 > i; i++) { NSNumber *t = [NSNumber numberWithInt:i]; [NSThread detachNewThreadSelector:@selector(doIt:) toTarget:self withObject:t]; } sleep(1); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(10, queue, ^(size_t i) { sleep(1); NSLog(@"GCD#%u",(int)i); }); } And the results: 2011-04-13 19:41:07.806 GDC[1494:5e03] Thread#0 2011-04-13 19:41:07

How to wait for a thread to finish in Objective-C

眉间皱痕 提交于 2019-11-28 16:53:03
问题 I'm trying to use a method from a class I downloaded somewhere. The method executes in the background while program execution continues. I do not want to allow program execution to continue until this method finishes. How do I do that? 回答1: Here's another way to do it using GCD: - (void)main { [self doStuffInOperations]; } - (void)doStuffInGCD { dispatch_group_t d_group = dispatch_group_create(); dispatch_queue_t bg_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);