나는 다음과 같은 함수를 호출 할 때 :아이폰 OS : dispatch_block_t 알고 코드의 일부를 두 번 실행하고 나는 그나마 왜
+ (void) myMethod:(NSString *) myConstString array: (NSArray *) myArray
{
dispatch_block_t block =
^{
for (int i = 0; i < myArray.count; i++)
{
if ([@"myString1" isEqual: myConstString])
// Do some easy job here
else if ([@"myString2" isEqual: myConstString])
// Do some other easy job here
[NSThread sleepForTimeInterval: 0.5];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:nil];
};
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.example.test", NULL);
dispatch_async(backgroundQueue, block);
}
[[NSNotificationCenter...]
가 두 번 실행됩니다. 나는이 통지를 "잡아라"책임이있는 다른 클래스의 메소드가 두 번 호출되기 때문에 그것을 알고있다. 첫 번째 전화는 즉시입니다 (그리고 이것은 나에게 이상합니다).
+ (void) myMethod:(NSString *) myConstString array: (NSArray *) myArray {
dispatch_block_t block =
^{
Boolean isForLoopExecuted = false;
for (int i = 0; i < myArray.count; i++)
{
if ([@"myString1" isEqual: myConstString])
// Do some easy job here
else if ([@"myString2" isEqual: myConstString])
// Do some other easy job here
[NSThread sleepForTimeInterval: 0.5];
isForLoopExecuted = true;
}
if (isForLoopExecuted)
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:nil];
};
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.example.test", NULL);
dispatch_async(backgroundQueue, block); }
addind 후 isForLoopExecuted
모든 propertly 작동 : 두 번째 통화는 약 2 초 (내가 :-)처럼이 호출) 이후에 I가 "수리"이 방법을 알고있다. 2 초 후에 한 번만 전화가 걸립니다. 이는 첫 번째 호출이 수행 될 때 for
루프가 실행되지 않음을 나타냅니다. 왜 이런 일이 일어나고 있는지 정말 궁금합니다. 시간 내 줘서 고마워!
코드 내의 다른 곳에서 동일한 알림을 보내지 않는다는 확인을 했습니까? 실행을 확인하기 위해 변수를 추가하는 것은 의미가 없습니다. 블록이 실행되는 경우 알림은 어쨌든 호출됩니다. – Gandalf