Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

ios - Make a synchronous HTTP call with RestKit

When logging a user into my application I need to pull a user object down from the server using only the username. This returns the userId (among other things) that I need in order to make other API calls. From that point I'll make a couple other HTTP calls using the userId. How can I make a synchronous call to completely pull down the user object before sending the other calls?

I've setup my object mapping in my app delegate class, which works perfectly, and am using this code to pull the user object down from the server:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/users/" stringByAppendingString:[_userNameField text]] delegate:self];

This is what I've tried... as suggested here: Making synchronous calls with RestKit

RKObjectLoader* loader = [[RKObjectManager sharedManager] objectLoaderForObject:currentUser method:RKRequestMethodPUT delegate:nil];
RKResponse* response = [loader sendSynchronously];

However this code (1) uses the deprecated method objectLoaderForObject and (2) crashes saying 'Unable to find a routable path for object of type '(null)' for HTTP Method 'POST''.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Putting aside the question of whether this is the ideal design for an iPhone application, I was able to accomplish what I was hoping using blocks.

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/users/" stringByAppendingString:[_userNameField text]] usingBlock:^(RKObjectLoader* loader) {

    loader.onDidLoadResponse = ^(RKResponse *response) {

        NSLog(@"Response: 
%@", [response bodyAsString]);
    };

    loader.onDidLoadObjects = ^(NSArray *objects) {

        APIUser *apiUser = [objects objectAtIndex:0];
        NSLog(@"user_id is %i", apiUser.user_id);


    };

    loader.onDidFailWithError = ^(NSError *error) {


            UIAlertView *badLoginAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"LOGIN_FAILED", nil)
                                                                   message:NSLocalizedString(@"BAD PASSWORD OR USERNAME", nil)
                                                                  delegate:self
                                                         cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                         otherButtonTitles:nil];
            [badLoginAlert show];           
    };
}];

Hope this helps someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...