funciona - porque mi iphone no agarra los datos moviles
¿Cómo uso NSConditionLock? O NSCondición (3)
Para aquellos que quieren una clase de prueba de muestra aquí, publico lo que hice para jugar y entiendo cómo funciona NSCondition.
// --- MyTestClass.h File --- //
@interface MyTestClass
- (void)startTest;
@end
// --- MyTestClass.m File --- //
@implementation MyTestClass
{
NSCondition *_myCondition;
BOOL _someCheckIsTrue;
}
- (id)init
{
self = [super init];
if (self)
{
_someCheckIsTrue = NO;
_myCondition = [[NSCondition alloc] init];
}
return self;
}
#pragma mark Public Methods
- (void)startTest
{
[self performSelectorInBackground:@selector(_method1) withObject:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(5);
[self performSelectorInBackground:@selector(_method2) withObject:nil];
});
}
#pragma mark Private Methods
- (void)_method1
{
NSLog(@"STARTING METHOD 1");
NSLog(@"WILL LOCK METHOD 1");
[_myCondition lock];
NSLog(@"DID LOCK METHOD 1");
while (!_someCheckIsTrue)
{
NSLog(@"WILL WAIT METHOD 1");
[_myCondition wait];
NSLog(@"DID WAIT METHOD 1");
}
NSLog(@"WILL UNLOCK METHOD 1");
[_myCondition unlock];
NSLog(@"DID UNLOCK METHOD 1");
NSLog(@"ENDING METHOD 1");
}
- (void)_method2
{
NSLog(@"STARTING METHOD 2");
NSLog(@"WILL LOCK METHOD 2");
[_myCondition lock];
NSLog(@"DID LOCK METHOD 2");
_someCheckIsTrue = YES;
NSLog(@"WILL SIGNAL METHOD 2");
[_myCondition signal];
NSLog(@"DID SIGNAL METHOD 2");
NSLog(@"WILL UNLOCK METHOD 2");
[_myCondition unlock];
NSLog(@"DID UNLOCK METHOD 2");
}
@end
// --- Output --- //
/*
2012-11-14 11:01:21.416 MyApp[8375:3907] STARTING METHOD 1
2012-11-14 11:01:21.418 MyApp[8375:3907] WILL LOCK METHOD 1
2012-11-14 11:01:21.419 MyApp[8375:3907] DID LOCK METHOD 1
2012-11-14 11:01:21.421 MyApp[8375:3907] WILL WAIT METHOD 1
2012-11-14 11:01:26.418 MyApp[8375:4807] STARTING METHOD 2
2012-11-14 11:01:26.419 MyApp[8375:4807] WILL LOCK METHOD 2
2012-11-14 11:01:26.419 MyApp[8375:4807] DID LOCK METHOD 2
2012-11-14 11:01:26.420 MyApp[8375:4807] WILL SIGNAL METHOD 2
2012-11-14 11:01:26.420 MyApp[8375:4807] DID SIGNAL METHOD 2
2012-11-14 11:01:26.421 MyApp[8375:4807] WILL UNLOCK METHOD 2
2012-11-14 11:01:26.421 MyApp[8375:3907] DID WAIT METHOD 1
2012-11-14 11:01:26.421 MyApp[8375:4807] DID UNLOCK METHOD 2
2012-11-14 11:01:26.422 MyApp[8375:3907] WILL UNLOCK METHOD 1
2012-11-14 11:01:26.423 MyApp[8375:3907] DID UNLOCK METHOD 1
2012-11-14 11:01:26.423 MyApp[8375:3907] ENDING METHOD 1
*/
Intento hacer que una función espere a otra, y me gustaría usar NSCondionLock para lograr esto. No estoy pidiendo ayuda, pero realmente espero que alguien me muestre un tutorial o ejemplo decente para explicar NSConditionLock, o posiblemente sugiera un método mejor.
NSConditionLock
ejemplo NSConditionLock
.
#import <Foundation/Foundation.h>
#define IDLE 0
#define START 1
#define TASK_1_FINISHED 2
#define TASK_2_FINISHED 3
#define CLEANUP_FINISHED 4
#define SHARED_DATA_LENGTH 1024 * 1024 * 1024
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:IDLE];
char *shared_data = calloc(SHARED_DATA_LENGTH, sizeof(char));
[NSThread detachNewThreadWithBlock:^{
[lock lockWhenCondition:START];
NSLog(@"[Thread-1]: Task 1 started...");
for (size_t i = 0; i < SHARED_DATA_LENGTH; i++) {
shared_data[i] = 0x00;
}
[lock unlockWithCondition:TASK_1_FINISHED];
}];
[NSThread detachNewThreadWithBlock:^{
[lock lockWhenCondition:TASK_1_FINISHED];
NSLog(@"[Thread-2]: Task 2 started...");
for (size_t i = 0; i < SHARED_DATA_LENGTH; i++) {
char c = shared_data[i];
shared_data[i] = ~c;
}
[lock unlockWithCondition:TASK_2_FINISHED];
}];
[NSThread detachNewThreadWithBlock:^{
[lock lockWhenCondition:TASK_2_FINISHED];
NSLog(@"[Thread-3]: Cleaning up...");
free(shared_data);
[lock unlockWithCondition:CLEANUP_FINISHED];
}];
NSLog(@"[Thread-main]: Threads set up. Waiting for 2 task to finish");
[lock unlockWithCondition:START];
[lock lockWhenCondition:CLEANUP_FINISHED];
NSLog(@"[Thread-main]: Completed");
}
return 0;
}
EDITAR: como comentó @Bonshington, esta respuesta se refiere a NSCondition
(a diferencia de NSConditionLock
):
- (void) method1 {
[myCondition lock];
while (!someCheckIsTrue)
[myCondition wait];
// Do something.
[myCondition unlock];
}
- (void) method2 {
[myCondition lock];
// Do something.
someCheckIsTrue = YES;
[myCondition signal];
[myCondition unlock];
}
El someCheckIsTrue
puede ser cualquier cosa, podría ser una simple variable BOOL o incluso algo como [myArray count] == 0 && color == kColorRed
, no importa. Solo importa que en un método verifique una condición mientras tenga el bloqueo y en otro método realice cambios que pueden hacer que la condición se haga realidad también mientras se tiene el bloqueo . La magia es la parte de wait
y signal
: la wait
realmente desbloquea el bloqueo y lo recupera después de algún otro hilo llamado signal
.