title: ios programming: Using threads to add multiple images to library
body: <p>In Xcode, when I'm trying to add <strong>more than 5 images</strong> to my library, it gives me the following error:</p>
  
  <pre><code>Error Domain=ALAssetsLibraryErrorDomain Code=-3301 "Write busy" UserInfo=0xa706aa0 {NSLocalizedRecoverySuggestion=Try to write again, NSLocalizedFailureReason=There was a problem writing this asset because the writing resources are busy., NSLocalizedDescription=Write busy, NSUnderlyingError=0xa770110 "Write busy"}
  </code></pre>
  
  <p>In order to solve this problem, I figured out threads would solve my problems. The documentation states that I can use POSIX threads or <code>NSThreads</code>. When I try using POSIX threads, I set my threads to be joinable, and I'm creating a void * function:</p>
  
  <pre><code>void * myFunc (void * image)
  {
         UIImageWriteToSavedPhotosAlbum((__bridge UIImage *)(image),self,nil,nil);
         pthread_exit(NULL);
         return NULL;
  }
  </code></pre>
  
  <p>I am also waiting for the thread to end. But still only 5 images are written.</p>
  
  <p>I've tried using <code>NSThreads</code> and did:</p>
  
  <pre><code>[self performSelectorOnMainThread:@selector(myFunc:) withObject:image waitUntilDone:YES];
  </code></pre>
  
  <p>But still it doesn't work.  </p>
  
  <p>Is there an answer to my problem? It's crucial to my work.</p>
  
  <p>Thanks.</p>
  
  <p>Edit:</p>
  
  <p>Tried dispatch_async too. Is it wrong?</p>
  
  <pre><code>dispatch_queue_t myQueue = dispatch_queue_create("com.cropr.myqueue", 0);
  
  for (UIImage * image in images) {
  
          dispatch_async(myQueue, ^{
  
              [self.library saveImage:image toAlbum:@"Cropr" withCompletionBlock:^(NSError *error) {
                  if (error!=nil) {
                      NSLog(@"Big error: %@", [error description]);
                  }
              }];
  
          });
  
      }
  </code></pre>
  
  <p>What do I need to add?</p>