2

I already post question How to use Bolts Framework[Facebook+Parse] but Now I've question, Must I use parse webservice if I want to use Bolts-framework?

They provide sample code like below which related(saveAsync:) to Parse webservice. But I've seen in this line "Using these libraries does not require using any Parse services. Nor do they require having a Parse or Facebook developer account" in Boltss' github

[[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;
}];

Now I get confusion, Is bolts framework need to use parse webservice?

Note: Don't ask where do you want to use Bolts-framework. see my first line of this question.

Community
  • 1
  • 1
Mani
  • 17,549
  • 13
  • 79
  • 100
  • No, you can use Parse just using the Parse SDK. No additional frameworks are needed. – rist Feb 06 '14 at 09:56
  • My friend, first read my question fully. Then ask question. I want to use bolts framework without help of parse framework. Is this any need to use parse framework? – Mani Feb 06 '14 at 09:59
  • My friend, first read the documentation of Bolts: "Using these libraries does not require using any Parse services. Nor do they require having a Parse or Facebook developer account." https://github.com/BoltsFramework/Bolts-iOS – rist Feb 06 '14 at 10:02
  • I know this. That's why I suggest you read my question above. What you post as command. I already added in my question. I've already that document fully.. – Mani Feb 06 '14 at 10:19
  • @rist Please suggest some way to clarify my question – Mani Feb 06 '14 at 10:20
  • Ok, still not sure if I understand your question. Bolts is not needed to use the services of the parse.com BAAS. Neither do you need to use/include the parse.com SDK in a project to be able to use Bolts. – rist Feb 06 '14 at 10:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46913/discussion-between-rist-and-imani) – rist Feb 06 '14 at 10:25
  • `saveAsync:` is just an example. It could be anything that would return a `BFTask` instance. [Read more about it here](http://blog.parse.com/2014/01/29/lets-bolt/) – Alladinian Feb 06 '14 at 10:52
  • @Alladinian I already see that. Instead of `saveAsync:`, How could we start with our own task? Do you have any idea about that? They didnt provide any other example.. – Mani Feb 06 '14 at 11:04

3 Answers3

1

Surely it doesn't need Parse webservice. I've the same difficulty in implementing my own task and I'm studying this framework. Take a look at BoltsTest code: you can find some useful code.

I'm trying some experiments in a sample project (https://github.com/giaesp/BoltsFrameworkSample). Basically you need to define your own method returning a BFTask. Here a simple excerpt.

- (BFTask*) parseHTML:(NSURL*)url searchString:(NSString*)searchString {
BFTaskCompletionSource * tcs = [BFTaskCompletionSource taskCompletionSource];

NSURLRequest * request = [NSURLRequest requestWithURL:url
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                      timeoutInterval:30];
NSURLResponse * response;
NSError * error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
    NSString * receivedData = [NSString stringWithUTF8String:[returnData bytes]];
    NSUInteger occurrences = [self countOccurencesOfString:@"iOS" inputString:receivedData];
    [tcs setResult:[NSNumber numberWithInt:occurrences]];


}
else {
    [tcs setError:error];
}

return tcs.task;
}

Then you can use your method as the docs explains and check the task status.

[[self parseHTML:[NSURL URLWithString:@"http://www.stackoverflow.com"]] continueWithBlock:^id(BFTask *task) {
if (task.isCancelled) {
    // the task was cancelled
 } else if (task.error) {
    // the task failed
} else {
    // the task completes
}
return nil;
}];
ilNero
  • 113
  • 2
  • 4
  • It would be helpful if you could give an example of the useful code instead of just linking to it. – Uyghur Lives Matter Feb 06 '14 at 16:50
  • Is it possible use this framework with third-party framework which used for webservices like ASHTTP or AFNetworking? I'am thinking like what you've posted. But I've written webservices with AFNetwork. Is it possible integrate bolts with that? Or This framework must use for other than else? – Mani Feb 10 '14 at 05:11
  • Your example code is pretty much useful. But I think, something is missing here. I couldn't see code in what you wrote above that is not related with Async operation. Could you explain about your code with Async operation? Anyway +1 to you? I think, this is initial point to start bolts-framework. – Mani Feb 10 '14 at 05:25
  • late to the game on this thread, and appreciate Mani's persistence. I too am new to Bolts and the examples are not quite sinking in. It seems like the BFTask here is simply being set AFTER the async call, which makes it no differing that setting bool's upon callback. I think many newbs are expecting that the task enfolds the async task. How else, for example, will it help actually cancel an async call given that BFTaskCompletionSource has a cancel method? – drew.. Jul 25 '15 at 13:59
1

I know it's been a while since this question was asked but as mani wanted to know if you could use Bolts framework with AFNetworking as well i want to add a quick example that shows usage.
It's written in swift and really just plain and simple.

func taskWithPath(path: String) -> BFTask {

    let task = BFTaskCompletionSource()
    AFHTTPRequestOperationManager().GET(path, parameters: nil, success: { (operation, response) in
        task.setResult(response)

    }) { (operation, error) -> Void in
        task.setError(error)
    }
    return task.task
}

Hope this helps :)

herby
  • 389
  • 3
  • 8
0

The idea with Bolts is to encapsulate any operation using a BFTask. You don't necessarily have to wrap the operation in a method, but it's a good way to imagine how you should structure your code:

- (BFTask*) asynchronousImageProcessOperation;
- (BFTask*) asynchronousNetworkOperation;

...and all of these would follow a similar pattern:

- (BFTask*) asynchronousNetworkOperation {
  BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];

  // ... here's the code that does some asynchronous operation on another thread/queue
  [someAsyncTask completeWithBlock:^(id response, NSError *error) {
    error ? [source setError:error] : [source setResult:response];
  }

  return task;
}

The beauty of it is that you can them string these tasks together in some way. For example, if you needed to process an image and then upload it, you could do:

[[object methodReturnImageProcessingTask] continueWithBlock:^(BFTask *task) {
  [[anotherObject imageUploadTaskForImage:task.result] continueWithBlock:^(BFTask *task) {
    self.label.text = @"Processing and image complete";
  }]
}]

Of course you could also encapsulate that two-stage task in its own task:

- (BFTask*) processAndUploadImage:(UIImage* image);

Typing from memory here. It's the sequencing and grouping that's really powerful. Great framework.

Jonathan Crooke
  • 912
  • 7
  • 19
  • thanks itsthejb. You have tried above is that handled with BFTask after completion of asynchronous task instead of during async task? AM I right? I want to know, how to use BFTask with Asynchronous task, especially where to lock and unlock resources with BFTask? – Mani Feb 20 '14 at 06:58
  • Ex: 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? – Mani Feb 20 '14 at 07:02