-1

The code below is called each time a scrollview scroll and if user scroll it multiple times, it crashed the code. How do i make sure only 1 code execute at a time or threadsafe?

 [self.cv addInfiniteScrollingWithActionHandler:^{
    [weakSelf loadNextPage];
}];
user1688346
  • 1,846
  • 5
  • 26
  • 48
  • Please provide a stack trace and explain *why* it's crashing. If you don't know *why*, then you need to find out before looking for answers to unknown questions. – trojanfoe Apr 24 '14 at 09:25

2 Answers2

0

Here is example

- (void)_startExperiment {  
      FooClass *foo = [[FooClass alloc] init];  
      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  

   for (int i = 0; i < 4; ++i) {  
     dispatch_async(queue, ^{  
       [foo doIt];  
     });  
   }  
   [foo release];  
 }

Detail is Here

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

The common pattern is to use a mutex to protect a critical section of code where the structure is accessed and/or modified.

just go through this link-> Does @synchronized guarantees for thread safety or not?

Community
  • 1
  • 1
Vizllx
  • 9,135
  • 1
  • 41
  • 79