1

My program:

- (void)getPostPraiseListWithSessionId:(NSString *)sid withPostId:(NSString *)postId withPageNo:(int)pageNo completeBlock:(void (^)(NSError *))complete{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{
                                 @"sid":sid,
                                 @"post_id":postId,
                                 @"pageNo":[[NSNumber alloc] initWithInt:pageNo],
                                 };
    [manager POST:[NSString stringWithFormat:@"%@%@",K_BASE_URL,K_GET_POST_PRAISE_LIST_URL] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        if([[responseObject objectForKey:@"state"] intValue] == 0){
            NSError *error = [NSError new];
            NSArray *postPraiseList = [MTLJSONAdapter modelOfClass:MSMPostPraiseInfo.class fromJSONDictionary:[responseObject objectForKey:@"data"] error:&error];
            complete(error);
        }else{
            NSError *error = [[NSError alloc] initWithDomain:MSMNETWORK_ERROR_DOMAIN code:[[responseObject objectForKey:@"state"] intValue] userInfo:@{@"message":[responseObject objectForKey:@"message"]}];
            complete(error);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        complete(error);
    }];

}

I have some questions.I wanna pass one parameter to the block named completeBlock,but I don't know what type of the parameter should I use.Should I use the weak type or the strong type?Whatever the type is weak or strong,please tell me the reason.

2
  • 1
    Provide more scope. Show an actual use of this parameter and point out exactly which value you are asking about. Commented Jun 21, 2015 at 15:11
  • In this case. I think pass complete(error); is enough. Commented Jun 21, 2015 at 15:21

2 Answers 2

2

Rob explained it a lot better, but long story short you use weak when you don't care much about the object being valid, allowing it to be dealloced before your block use it. strong should be used when the object must be valid (and in memory) to be used inside your block.

Also, here are some minor improvements you could do (which not really related to your question):

  • [[NSNumber alloc] initWithInt:pageNo] can be replaced by a simple @(pageNo)
  • If K_BASE_URL and K_GET_POST_PRAISE_LIST_URL are declared in #define as NSString* macros, you don't need to use stringWithFormat:, you can simply use [manager POST: K_BASE_URL K_GET_POST_PRAISE_LIST_URL parameters:(...)]
  • In this case, your NSError *error = [NSError new]; would probably better off as NSError *error = nil;, but it depends on what that method of MTLJSONAdapter does.
Sign up to request clarification or add additional context in comments.

Comments

1

Frequently you will use a weak reference because you often do not want the request to maintain a strong reference to the object in question. For example, if you're updating a view object in your user interface, if the view is dismissed before the request finishes, there's no benefit in having the network request keeping a strong reference to a user interface control that is no longer visible.

Sometimes, when dismissing a view controller, it is useful to cancel any pending network requests that are only being used for the benefit of that scene. In that situation, you would want to save the reference to the AFHTTPRequestOperation object returned by POST method and then cancel that request in dealloc of the view controller. But that only works if you explicitly avoid having the blocks maintain a strong reference back to the view controller, i.e. you used weak references.

In your case, you would appear to be posting data, so maybe canceling the request isn't necessary, so this latter point might not be relevant. But, nonetheless, you may well still use weak references if you're only referring to UI controls that may be dismissed while the request is running. But if you're calling some additional instance methods that are updating a persistent store with the result of the request upon completion of the request, then you might need strong reference.

Bottom line, the basic question is whether it is critical for the object in question to be retained for the duration of the network request (i.e. the completion block would have a strong reference) or not. Generally you don't need that, and would thereby favor weak reference. But we can't speak to your specific case without more information about what you plan on doing within the closure you pass to getPostPraiseListWithSessionId method. The choice of strong or weak is not a function of the getPostPraiseListWithSessionId method, but rather a question of what you plan on doing inside the completion block you supply to this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.