How to use Bolts Framework[Facebook+Parse]

13,906

Bolts is great. Agree that the documentation is a little unfocussed right now. Here's a quick example, though:

// the completion source is the *source* of the task...
BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];
[self asynchronousMethodWithCompletion:^(id response, NSError *error) {
   // your task completed; inform the completion source, which handles
   // the *completion* of the task
   error ? [source setError:error] : [source setResult:entity];
}];

[source.task continueWithBlock:^id(BFTask *task) {
   // this will be executed after the asynchronous task completes...
}];

I've found it especially useful for group completions (semi pseudo-code):

NSMutableArray *tasks = @[].mutableCopy;
loop {
  BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];

  // ...make a task similar to above...

  [tasks addObject:source.task];
}

// now the group completion:
BFTask *groupTask = [BFTask taskForCompletionOfAllTasks:tasks.copy];
[source.task continueWithBlock:^id(BFTask *task) {
   // this will be executed after *all* the group tasks have completed
}];

This is a neater way of doing what you might otherwise do with dispatch groups. There's plenty more to it, though, in terms of sequencing your tasks in both serial and parallel, etc. etc.

Hope that helps you get started.

Share:
13,906
Mani
Author by

Mani

Working as Sr. IOS Engg at MVI Technology with 9+ exp. Experience in Swift, Objective-C, Android. Worked with Rolo for Android @ Netmine Mobile Innovations. I've published Tattle-UI-IOS control. It is an testing kit used to beta-tester for reporting UI issue by marking on current screen's snapshot.(like UI misalignment, font..etc). Earned Marshal badge at No.884.

Updated on July 25, 2022

Comments

  • Mani
    Mani almost 2 years

    Just Now I see this announcement from Facebook about Bolts Framework for IOS.

    I can see this as main concept:

    The first component in Bolts is “tasks”, which make organization of complex asynchronous code more manageable

    But I didn't get this. I got confused about Bolts framework. How to use it(whether its related to web service or to JSON response parsing).

    They provided examples with ParseObject with parse SDK but I don't know about it and they didn't provide any example with Xcode project.

    Facebook provided explanation about this. But I can't figure out how to integrate with my project.

    Code they provided is very confusing:

    [[object saveAsync:obj] continueWithBlock:^id(BFTask *task) {
      if (task.isCancelled) {
        // the save was cancelled.
      } else if (task.error) {
        // the save failed.
      } else {
        // the object was saved successfully.
        SaveResult *saveResult = task.result;
      }
      return nil;
    }];
    

    We can download bolts-framework here. Can anyone please explain more about this?

    Update: Here I received some answer about new question of bolts.

    Examples: Assume you want to load all images from AssertLibrary and resize all images to standard size while loading, so it will struck if do with main thread. In this place, If you go with async operation means, How to use BFTask with it? Another Ex. In one time, you are trying to call 10 webservice parallel with async operation, how could you use GCD with BFTask?