Question

I've got a function downloading something from the internet. Then it maps the items and returns everything to the controller. The problem is that there may be a lot of items (facebook don't let to divide them) and i have to map them in the background. However the user can quit the controller already and i don't know how to check if my success() owner still exists.

Is it a good solution to put the mapping in the dispatch at all...?

Call:

[[MyHTTPClient sharedInstance] getSomethingWithAbc:abc successBlock:^{
  [self reloadMyView];
} failureBlock:^(NSError *error) {
  //nothing atm
}];

where:

@interface MyHTTPClient : AFHTTPClient
+ (OHTTPClient *)sharedInstance;
- (void)getSomethingWithAbc:(ABC *)abc successBlock:(void (^)())success failureBlock:(void (^)(NSError *error))failure;

Method implementation:

- (void)getSomethingWithAbc:(ABC *)abc successBlock:(void (^)())success failureBlock:(void (^)(NSError *error))failure {
    // request something from facebook API, wait for response then run:
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        // here map these items - there may be a lot of them so i have to do it in background
        dispatch_async(dispatch_get_main_queue(), ^{
            success(); // this may call a function from controller that does not exist already
        });
}
Was it helpful?

Solution

However the user can quit the controller already and i don't know how to check if my success() owner still exists.

What do you mean if the owner "still exists"?

self is retained by the block. That means that the "owner" will "still exist" as long as the block exists. So if you call success() and the controller is no longer showing, well, it will reloadMyView, which (depending on what your functions do) will "reload" a view that is no longer showing. Which probably doesn't matter.

What if you want to not call reloadMyView at all? Well, you can let the block capture a weak reference to the "owner". That won't prevent it from being deallocated as it normally would. Then, when you call the success block, and the object is deallocated, the weak reference you are using will have value nil, and sending a message to nil does nothing.

You can choose which way you want.

OTHER TIPS

This is fine, but you should have a cancel operation so that, before calling success, you can check if the operation has been cancelled.

You'd better write a network processing module is independent of your controller,and set delegate method as - (void)didSuccessWithResult:(NSDictionary*)result; - (void)didFailWithError:(NSError*)error;

or Send Notification

In you network processing module you can define download work with MyHTTPClient to complete the proxy method

Hope helpful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top