Giter VIP home page Giter VIP logo

backgroundloopprocessor's Introduction

BackgroundLoopProcessor

An objective-c class to run a loop on the main thread, while still allowing UI updates during processing.

Use case

In some cases, you may need to carry out UI-intensive operations in a loop, but still want to be able to update the UI to indicate progress. This is a problem in Cocoa because all UI operations must be carried out on the main thread. This means that carrying out UI operations in a loop will block the main thread, meaning you cannot update the UI to indicate progress to the user. This simple class addresses that problem by providing a simple function for running a loop on the main thread, and accepts three blocks as arguments. These blocks allow you to specify the code for processing iterations of the loop, how the UI should be updated in response to the processing of the loop, and a completion block.

Usage

BackgroundLoopProcessor is extremely simple to use. Simply #include "BackgroundLoopProcessor.h" in the class you want to use it, and then call +[BackgroundLoopProcessor processLoopWithRunCount:processingBlock:updateUIBlock:completionBlock:];. BackgroundLoopProcessor will then carry out the following operations:

  1. Call updateUIBlock first, to show an initial progress state
  2. Schedule the running of processingBlock for the next run loop
  3. On the next run loop (and therefore after the UI has updated to show progress), call processingBlock and pass the run number to the block (equivalent to the count variable in a for loop)
  4. Schedule updateUIBlock and processingBlock for the next run loop
  5. Repeat this process until the loop is completed
  6. Call completionBlock

Example

// MyClass.m

#import "BackgroundLoopProcessor.h"

- (IBAction)startprocessing {
    [BackgroundLoopProcessor processLoopWithRunCount:self.myArray.count processingBlock:^(NSInteger run) {
    
        //Get an object from the array using the `run` variable as the index
        id object = [self.myArray objectAtIndex:run];
        
        //Do something with `object`

    } updateUIBlock:^(NSInteger run) {
    
        //Update the UI to show progress

    } completionBlock:^{
    
        //Update the UI to show processing complete

    }];
}

backgroundloopprocessor's People

Contributors

mashers avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.