Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...