Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:18:16+00:00 2026-06-08T22:18:16+00:00

I am trying to write a wrapper for RestKit , so that all requests

  • 0

I am trying to write a wrapper for RestKit, so that all requests call one function which in turn would trigger a request via RestKit.

Here’s what I have so far:

A function would call my wrapper as follows:

NSDictionary *response = [Wrappers sendRequestWithURLString:url method:@”GET”];

And my wrapper methods:

+ (NSDictionary *)sendRequestWithURLString:(NSString *)request method:(NSString *)method
{    
    RKRequestDidFailLoadWithErrorBlock failBlock;

    if ([method isEqualToString:@"GET"])
        return [self sendGETRequestWithURLString:request withFailBlock:failBlock];

    else if ([method isEqualToString:@"POST"])
        return [self sendPOSTRequestWithURLString:request withFailBlock:failBlock];

    return nil;
}




   + (NSDictionary *)sendGETRequestWithURLString:(NSString *)request withFailBlock:(RKRequestDidFailLoadWithErrorBlock)failBlock {

        RKObjectManager *manager = [RKObjectManager sharedManager];

        __block NSDictionary *responseDictionary;

        [manager loadObjectsAtResourcePath:request usingBlock:^(RKObjectLoader *loader) {

            loader.onDidLoadResponse = ^(RKResponse *response) {

                [self fireErrorBlock:failBlock onErrorInResponse:response];

                RKJSONParserJSONKit *parser = [RKJSONParserJSONKit new]; 
                responseDictionary = [[NSDictionary alloc] initWithDictionary:[parser objectFromString:[response bodyAsString] error:nil]];
            };
        }];

        return responseDictionary;
    }

+ (void)fireErrorBlock:(RKRequestDidFailLoadWithErrorBlock)failBlock onErrorInResponse:(RKResponse *)response {

    if (![response isOK]) {

        id parsedResponse = [response parsedBody:NULL];
        NSString *errorText = nil;

        if ([parsedResponse isKindOfClass:[NSDictionary class]]) {
            errorText = [parsedResponse objectForKey:@"error"];
        }

        if (errorText)
            failBlock([self errorWithMessage:errorText code:[response statusCode]]);
    }
}

+ (NSError *)errorWithMessage:(NSString *)errorText code:(NSUInteger)statusCode {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                    message:@"Please make sure you are connected to WiFi or 3G." 
                                                   delegate:nil
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];

    return nil;
}

The problem here is when responseDictionary returns, the value is nil since onDidLoadResponse would not have processed yet as it runs concurrently.

In this case, what would be the best approach in setting responseDictionary? I’m trying to avoid calling a setter method of another class. In this case, is my only option using delegates, which defeats the whole purpose of creating a wrapper class since RestKit calls require usage of delegate methods to return the response?

Would I be able to pass my wrapper a success block which would update some local ivar? How would I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:18:17+00:00Added an answer on June 8, 2026 at 10:18 pm

    You pass a success block as you have said. Here is an example of how to do that:

    .h

    typedef void (^kServiceCompleteBlock)(NSDictionary* responseDictionary);
    
    ...
    + (NSDictionary *)sendGETRequestWithURLString:(NSString *)request withFailBlock:(RKRequestDidFailLoadWithErrorBlock)failBlock completion: (kServiceCompleteBlock) completion ;
    
    ...
    

    .m

    + (NSDictionary *)sendGETRequestWithURLString:(NSString *)request withFailBlock:(RKRequestDidFailLoadWithErrorBlock)failBlock completion: (kServiceCompleteBlock) completion {
    
    ...
    
    loader.onDidLoadResponse = ^(RKResponse *response) {
    
                [self fireErrorBlock:failBlock onErrorInResponse:response];
    
                RKJSONParserJSONKit *parser = [RKJSONParserJSONKit new]; 
                responseDictionary = [[NSDictionary alloc] initWithDictionary:[parser objectFromString:[response bodyAsString] error:nil]];
    
                if( completion )
                   completion(responseDictionary);
            };
    
    ...
    
    } 
    

    However, let me warn you of a potential design flaw. You should design your app so that the UI is driven by your data and not your web service. This means your UI should automatically update when your data model is updated and not when your web service returns. Be careful how you use this response dictionary. If it is to update UI then you are running down a dangerous road.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some wrapper class or function that allows me to execute
I'm trying to write a simple wrapper class in F# that takes a function
I'm trying to write a wrapper function for read() system call , using asm
I'm trying to write a SWIG wrapper for a C library that uses pointers
I am trying to write a simple wrapper around a connection pointer that will
I am trying to write a wrapper shell script that caches information every time
I am trying to write a wrapper which will execute a script as a
I'm trying to write classes which handle different number types. I know that C#
I'm trying to write a Java function that takes a Class<?> and returns a
I am trying to write a wrapper function to figure out who is calling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.