Is Bolts framework[Parse+Facebook] need to use parse webservice?

和自甴很熟 提交于 2020-01-15 04:52:19

问题


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.


回答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;
}];



回答2:


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 :)




回答3:


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.



来源:https://stackoverflow.com/questions/21599172/is-bolts-frameworkparsefacebook-need-to-use-parse-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!