title: Separate API-class for Parse backend (IOS)
body: <p>I am new with Parse and Ios-development.</p>
  
  <p>I develop a ios-app that use Parse as backend. </p>
  
  <p>I have got the main-function to work now, but i have a BIG problem.</p>
  
  <p>I want to create a separate class for my API-handling to Parse. As i set it up now i have my parse-code directly in my view-controllers and as far as i know that not that nice coding.</p>
  
  <p>But, the issue is to handle the background-jobs. Let say if i want to do a GET from the server, this can be done in a background-thread, just using "findObjectsInBackgroundWithBlock"</p>
  
  <p>The problem is when i move this method to a separate API-class. Then my ViewController ask my API-class to get all the objects an the API-class will return it as soon its done. It will nor run in the background, i cant return a NSMutableArray with objects to the viewController until the fetch is done.</p>
  
  <p>I have thinking that i maybe can get the data from parse synchronously in my API-class by using [query findObjects:&amp;error] , as long as i figure out how to create my get-method in the API-class to run asynchronously.</p>
  
  <p>I have try to create my API-method as a asynchronously method using blocks but will not run in background on a separate thread. (I am new to blocks an dont evan no if thats the correct way to crate a method that will run in a separate thread when using it)</p>
  
  <p>Here is my API-method (Class: <em>APIClient</em>)</p>
  
  <pre><code>    + (void) GETAllShoppingGroups:(void (^)          (NSMutableArray*))completionBlock{    
  
      //Create a mutable array (nil)
      NSMutableArray *shoppingGroupsArray = nil;
  
      //Create query for class ShoppingGroupe
      PFQuery *query = [ShoppingGroupe query];    
  
      //Filter - find only the groups the current user is related to
      [query whereKey:@"members" equalTo:[PFUser currentUser]];
  
      //Sort Decending
      [query orderByDescending:@"createdAt"];
  
      //Tell Parse to also send the real member-objects and not only id
      [query includeKey:@"members"];
  
      //Send request of query to Parse with a "error-pointer"and fetch in a     temp-array
      NSError *error = nil;
      NSArray *tempArray = [NSArray arrayWithArray:[query findObjects:&amp;error]];
  
      //Check for success
      if (!tempArray) {
      NSLog(@"%@", error);
      NSLog(@"ERROR: %@", [error userInfo][@"error"]);
      return completionBlock(shoppingGroupsArray);
      } else {
      //Seccess
      shoppingGroupsArray = tempArray.mutableCopy;
      completionBlock(shoppingGroupsArray);
      }
  }
  </code></pre>
  
  <p>Here is my ViewController Class (Class: <em>ShoppingGruopViewController</em>)</p>
  
  <pre><code>  - (void) getAllObjects{
  
     //Init array if nil
     if (!self.shoppingGroupeArray) {
     self.shoppingGroupeArray = [[NSMutableArray alloc]init];
     }
  
     //Remove old objects
     [self.shoppingGroupeArray removeAllObjects];
  
     //Get objects
     [APIClient GETAllShoppingGroups:^(NSMutableArray* completionBlock){
     if (completionBlock) {
     [self.shoppingGroupeArray addObjectsFromArray:completionBlock]; }
  
     [self.tableView reloadData];
     }];
  } 
  </code></pre>