nsthread

Keep an NSThread containing an NSTimer around indefinitely? (iPhone)

跟風遠走 提交于 2019-12-05 07:24:10
问题 I have some web service data in my app that needs to be updated every 3 minutes. I had tried out a few approaches but got a really good piece of advise in here last week, I should not build a new thread every 3 minutes and then subsequently try and dealloc and synchronize all the different parts so that I avoided memory bug. Instead I should have a "worker thread" that was always running, but only did actual work when I asked it too (every 3 minutes). As my small POC works now, I spawn a new

Thread and NSTimer

天大地大妈咪最大 提交于 2019-12-05 05:24:13
问题 I am making an application that has a timer. I count the minutes and seconds from a given time down to 0. When this happen I launch an alertview. My structure is this: Mainthread method allocate a new thread and initialize it. The entrypoint(method) for the thread has a timer which invokes a method for calculating time left, and if the time is up, displays an alertview. However, is this correct? Because now I am updating GUI from another thread than the main...and that is bad right? And I am

dispatch_semaphore_t reuse - What am I missing here?

余生颓废 提交于 2019-12-05 03:46:54
I have some code where I am using dispatch_semaphore_t to signal operation completion. When the semaphore is a member variable, it does not seem to behave correctly. I will show example code that works and an example that does not seem to work: @implementation someClass { dispatch_semaphore_t memberSem; dispatch_semaphore_t* semPtr; NSThread* worker; BOOL taskDone; } - (id)init { // Set up the worker thread and launch it - not shown here. memberSem= dispatch_semaphore_create(0); semPtr= NULL; taskDone= FALSE; } - (void)dealloc { // Clean up the worker thread as needed - not shown here. if(

scheduling a Thread after a Thread in iphone application

不打扰是莪最后的温柔 提交于 2019-12-05 01:23:16
问题 I want to schedule a thread after a thread completion. Is it possible ? How? For example ( to specify my need ) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 1. response - schedule myThread // 2. response - schedule a new thread which will be executed after myThread // 3. response - schedule a new thread which will be executed after second thread // ..... } 回答1: If you use NSOperations you can use the addDependency: method to specify an operation's

pass block as parameter in my case

情到浓时终转凉″ 提交于 2019-12-04 22:06:27
I have a function which takes a block as parameter: typedef void (^ MyBlock)(int); -(void)doTask:(MyBlock)theBlock{ ... } I need to run above function on another thread, I want to use - performSelector:onThread:withObject:waitUntilDone: , my current code: NSThread *workerThread = [[NSThread alloc] init]; [workerThread start]; [self performSelector:@selector(doTask:) onThread:workerThread withObject:??? waitUntilDone:NO]; BUT, How can I pass MyBlock parameter with this approach? (Please don't suggest GCD, I am wondering how can I do with my current code, is it possible?) This answer assumes you

How to achieve concurrent task through NSOperation? [closed]

天涯浪子 提交于 2019-12-04 19:33:43
I am new to NSoperation . NSoperation is a singleshot object. How can we achieve multiple operations concurrently through NSoperation ? is it possible without NSoperationQueue ? Even if we use NSoperationQueue, it will perform operations FIFO format.How will it execute Concurrently? If you want to implement a concurrent operation —that is, one that runs asynchronously with respect to the calling thread—you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the

Delegate callback from other NSThread

依然范特西╮ 提交于 2019-12-04 17:21:59
I have object with .delegate property which i manipulate in method 'doJob'. I assign this property with 'self' and my function is being called when this object finishes his job. Till now everything is fine. Now i want to manipulate this object in a separate thread. I'm using [NSThread detachNewThreadSelector...] to run the 'doJob' function. In this case my delegate method not being called. I guess this is because 'self' points to new thread instead of main one. Ok. I'm passing self as argument to function while creating the thread and it still not working. What do i miss? my current code is as

po [NSThread currentThread]

霸气de小男生 提交于 2019-12-04 17:15:28
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. 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 thread ID from that API. [NSThread currentThread] is about as unique of a number as you'll get. If the

Detect end of performSelectorInBackground:withObject:

你说的曾经没有我的故事 提交于 2019-12-04 10:40:36
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? 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]; } Nathan Jones If you just want to know when it's finished (and don't want to pass much data back - at which point I would recommend a delegate) you could simply post a notification to the notification

How to make NSRunLoop work inside a separate thread?

末鹿安然 提交于 2019-12-04 10:13:16
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, NSStringFromClass([AppDelegate class])); } } The function method is called but there's no 20 seconds pause