Skip to main content

Running Database Fetch (Core Data) in Background Thread using magical Record


  To run database fetching in background thread(in a separate context), so that the UI does not get blocked:

__block NSMutableArray *dataArray= [[NSMutableArray alloc] init];
  // Create a child context of the main context
  NSManagedObjectContext *privateContext = [NSManagedObjectContext MR_context];
  // When using private contexts execute the core data code in it's private queue using performBlock: or performBlockAndWait:
  [privateContext performBlock:^{
    // Execute fetch
  NSArray *privateObjects;
  if(entity_predicate == nil) {
      //fetch all! duh!
      privateObjects = [self MR_findAllSortedBy:sort_key ascending:YES inContext:privateContext];
    } else {
      privateObjects = [self MR_findAllSortedBy:sort_key ascending:YES withPredicate:entity_predicate inContext:privateContext];
    }
   
    // Convert fetched objects into object IDs which can be pulled out of the main context
    NSArray *privateObjectIDs = [privateObjects valueForKey:@"objectID"];
    // Return to main thread
    dispatch_async(dispatch_get_main_queue(), ^{
      // Create a new predicate to pull objects out
      NSArray *finalResults;
      if(privateObjectIDs) {
      NSPredicate *mainPredicate = [NSPredicate predicateWithFormat:@"self IN %@", privateObjectIDs];
      // Execute fetch
        finalResults = [self MR_findAllWithPredicate:mainPredicate];
      }
      // use finalResults from the main thread
      [dataArray removeAllObjects];
      dataArray = [finalResults mutableCopy];
      if (fcb) {
        fcb(dataArray);
      }
     
    });
  }];

Comments

Popular posts from this blog

Copying Multiple Content Simaltaneously

We often used to thank   Larry Tesler    for his splendid invention "Cut/Copy/Paste". Think of the world without copy/paste :o :o :o Have you ever tried copying non-sequential content? Ctrl+v will paste the last copied content in the clipboard, whereas ctrl+c copies the content that are selected sequentially   to the clipboard. What if I wanted to copy multiple content from multiple web pages and pasting it in one go, instead of pasting one by one? Or without using paste command copy the multiple content directly to a file? Here is the solution for the Mac Users. [Windows user? Working on it. Will update asap ;) ]   Follow the steps below, which takes less than five minutes and save several minutes ;) 1)     Launch “Automator” 2)     Choose “Utilities” in the left pane 3)     Double Click “Run Shell Script” 4)     The “Run Shell Script” will be added to t...

Separator Inset Problem - iOS 8

                    In iOS8, there is extra space in table view separators by default. It is nothing but the edge inset. To remove the separator edge inset, in iOS 7, custom separator insets can be set in the attributes inspector of the table view. In iOS 8, use the following in the table view controller: //set edge inset zero for default separator -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath     {     if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {     [self. tableView setSeparatorInset:UIEdgeInsetsZero];   }     if ([self. tableView respondsToSelector:@selector(setLayoutMargins:)]) {     [self. tableView setLayoutMargins:UIEdgeInsetsZero];   }     if ([cell respondsToSelector:@selector(setLay...

Unity Upgrade 2017.2 and above

Hello  Unity Developers , Recently, I faced few issues with Unity Editor and I hope sharing it will help. What was I trying to do? Scenario? Upgrading to Unity 2017.2 & Unity 2017.3 I was trying to upgrade one of my project from Unity 2017.1.2 version to Unity 2017.2.1f1. On doing so, usually unity automatically updates the obsolete scripts to support new APIs. Once it got updated, I tried to play the game in editor and it worked. Then, what was the problem? While building the project to UWP application, there were some compile errors where Unity.VR need to be updated to Unity.XR. I fixed those errors, quit unity and tried building again.  Wait, Why did I quit Unity and then built it again? Its because unity has this weird issue, when there are compiler errors. Though I had fixed the errors, on building it again, the errors will not go away. We have to quit unity and then build it, TADA, the error will go away. Now coming back to other is...