title: iOS App Architecture with NSOperations
body: <p>Two month ago I started to write a new iPhone Application and for this reason I created a generic RESTFul web service, which allows me to have a lot of these necessary features like user authentication, user profiles, a friendship system, media processing, a messaging system and so on. In my mind there are several use cases to reuse this webservice for future iPhone applications. </p>
  
  <p>With this state of mind, I decided to write a static library for this application (and all future apps) that handles all the heavy lifting like media (image, video, sound) setup and processing, communicating with the web service, parsing and mapping of the results, handling CoreData and so on.</p>
  
  <p>Given my application there are scenarios when a lot of parallel tasks are running (worst case) e.g.
  the user currently changes his/her profile picture, while the application sends the users location to the server (in the background) and a new push notification is received. </p>
  
  <p>So decided to encapsule each logical operation (like SendUserLocation or GetCurrentFriendList) in a NSOperation and add them to a serviceQueue (NSOperationQueue). </p>
  
  <p>Each Operation is able to spawn subtasks when the operation successfully got a result from the webservice and should process it now.</p>
  
  <p><img src="https://i.stack.imgur.com/TVvQH.png" alt="alt text"></p>
  
  <p>A typical ServiceManager method looks like</p>
  
  <pre><code>- (void)activateFriendsSync:(id)observer onSuccess:(SEL)selector {
      ELOSyncFriends *opSyncFriends  = [[ELOSyncFriends alloc] initWithSM:self];
      [self ELServiceLogger:opSyncFriends];
      [serviceQueue addOperation:opSyncFriends];
      if(observer) {
          [self registerObserver:observer selector:selector name:opSyncFriends.notificationName]; 
      }
  }
  </code></pre>
  
  <p>Each operation, request (to the server) and subTask uses a GUID as a notificationName to notify the parent object when it's done processing. If everything in an operation is done, it sends a notification back to the user interface.</p>
  
  <p>That said, the code for adding and removing subtasks looks like this</p>
  
  <pre><code>- (void)removeSubTask:(NSNotification*)notification {
      ELRequest *request = (ELRequest*)[notification object];
      [subTasks removeObjectIdenticalTo:request.notificationName];
      if([subTasks count] == 0) {
           // all SubTaks done, send notification to parent
          [serviceManager.notificationCenter postNotificationName:self.notificationName object:request];
      }
  }
  
  - (NSString*)addSubTask {
      NSString* newName = [self GetUUID];
      [subTasks addObject:[newName retain]];
      [serviceManager.notificationCenter addObserver:self selector:@selector(removeSubTask:) name:newName object:nil];
      return newName;
  } 
  
  - (NSString *)GetUUID {
      CFUUIDRef theUUID = CFUUIDCreate(NULL);
      CFStringRef string = CFUUIDCreateString(NULL, theUUID);
      CFRelease(theUUID);
      return [(NSString *)string autorelease];
  }
  </code></pre>
  
  <p>Now all I've got to do is to call the serviceManager in my gui to start a specific operation like </p>
  
  <pre><code>[self.core.serviceManager activateFriendsSync:nil onSuccess:nil];
  </code></pre>
  
  <p>If I want to register an observer, I just pass an observer object and a selector like this</p>
  
  <pre><code>[self.core.serviceManager activateFriendsSync:self onSuccess:@selector(myMethod:)];
  </code></pre>
  
  <p><strong>Last but not least my question(s)</strong>: The "architecture" runs very well and stable, but is it worth doing? Does it create too much overhead? Does it even make sense? How you, personally, implement concurrent operations?</p>
  
  <p>Best
  Henrik</p>
  
  <p>P.S. Feel free to edit my question, ask questions (as a comment), call me names for this thinking. </p>
  
  <p>I really had a hard time explaining it, basically because I'm not a native english speaker. And don't get me wrong. I didn't write this posting to show off in any kind. All I want to do is learn (and maybe to write a more advanced iphone / objective c question)</p>