nsthread

Multiple NSThreads synchronization

风流意气都作罢 提交于 2019-12-08 02:06:09
问题 I want to achieve the following task using NSThread I've a main thread and three (3) workerThread T1, T2, T3. All these started at the same time from main thread, Main thread has an int size variable. Now I want to synchronize all three threads in a way they, when my each of the above threads will execute, it will print the following: //in main thread - (void) mainFunction{ size = 0; NSThread* T1 = [[NSThread alloc] initWithTarget:self selector:@selector(workerThread:) object:@"T1"]; [T1

OS X Cocoa timer not fired when application on background

…衆ロ難τιáo~ 提交于 2019-12-07 14:32:56
问题 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? 回答1: Use beginActivityWithOptions:reason: to disable app nap for your

Running NSTimer on a thread

孤者浪人 提交于 2019-12-07 12:38:58
问题 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

How can i stop the background thread from the main thread in Iphone NSthread?

笑着哭i 提交于 2019-12-07 11:27:28
I start a background thread from my program while my main thread is checking something. If the checking goes false, I want to stop the running background thread and start it all over again with my new parameters. I want to stop it coz the thread will be downloading images and if I doesnt stop it and call it again, it will take a lot of time for the new images to get downloaded. Is it possible to do so? In my experience, its usually best to let a thread exit gracefully by itself. How about having some common variable that the main thread could set to false if things go awry. The second thread

Difference between Sleep and NSRunLoop runMode:beforeDate:

谁说我不能喝 提交于 2019-12-07 10:36:19
问题 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? 回答1: Yes, NSRunLoop is better because it allows the runloop to

iPhone SDK - Running a repeating process in a background thread

孤人 提交于 2019-12-07 10:31:43
问题 I have an iPhone application which in which I want to perform a method in the background every 1 second. So in my main thread, I have the following code upon my UIViewController viewDidLoad() : [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatedMethod) userInfo:nil repeats:YES]; with the following methods: -(void)repeatedMethod { if(!processIsRunning) { processIsRunning = YES; [self performSelectorInBackground:@selector(runProcessInBackground:) withObject:myImage]; } }

Best multithreading approach in Objective C?

孤人 提交于 2019-12-07 02:54:24
问题 I'm developing an iPad-app and I'm currently struggling with finding the best approach to multithreading. Let me illustrate this with a simplified example: I have a view with 2 subviews, a directory picker and a gallery with thumbnails of all the images in the selected directory. Since 'downloading' and generating these thumbnails can take quite a while I need multithreading so the interaction and updating of the view doesn't get blocked. This is what I already tried: [self

Pause Main thread until background thread retrieves user input

半腔热情 提交于 2019-12-06 16:06:20
In my main starting thread I need to pause the code and start a new thread and wait until I get user input. Then Id like to discard the new thread made and go back to where the main thread left off. But whats happening is that the new thread is called but the main thread keeps going with the code. How do I deal with this without interfering with the user being able to use the interface buttons? I think that maybe another nscondition needs to be made in my if(moveCount == 2) statement? Or that my main thread needs to wait for a signal from my other thread notifying user input is received.

Objective C - performSelectorInBackground V.S detachNewThreadSelector?

元气小坏坏 提交于 2019-12-06 11:50:09
问题 Both detachNewThreadSelector and performSelectorInBackground are used to call a method in the background. Is there any difference between the 2 methods? or do they both work the same way? 回答1: They're both essentially the same but slightly different paradigms. Behind the scenes they do exactly the same thing. The only real difference is that -[performSelectorInBackground:withObject:] follows all the other performSelector style methods in that they're defined on NSObject and you actually

Delegate callback from other NSThread

好久不见. 提交于 2019-12-06 09:55:21
问题 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